2

I'm new to programming so I guess this question is kinda stupid. I want to check if current Text exists in Array of Labels, because I get same name twice sometimes.

for (int i = 0; i < DM.checkedListBox1.CheckedItems.Count; i++)
{
    labels[i] = new Label();
    for (int j = i; j < DM.dataGridView1.Columns.Count; j++)
    {
        if (DM.dataGridView1.Columns[j].Visible)
        {                        
            if (labels.Contains(DM.dataGridView1.Columns[j].HeaderText))
            {
                labels[i].Text = DM.dataGridView1.Columns[j].HeaderText;
                break;
            }
        }
    }
}

Thank you!

1 Answers1

0

If you want to know if array of labels contains particular text, you need to search through their Text property:

labels.Select(l => l.text).Contains(DM.dataGridView1.Columns[j].HeaderText)

I used Select LINQ extension method to change array of Labels into array of strings.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69