Eugene Chefranov
Eugene Chefranov

Reputation: 171

How can I loop through many ComboBoxes with similar name?

I have many ComboBoxes with similar name like ComboBox1, ComboBox2, ComboBox3 and etc. How can I looping through these ComboBoxes and get some parameters of them.
In VB.NET it's like that:

For k As Integer = 1 To 50
    Console.WriteLine(Me.GroupBox1.Controls("ComboBox" & k).SelectedIndex)
Next

Can I do something like that on C#?

Upvotes: 0

Views: 57

Answers (1)

AJITH
AJITH

Reputation: 1175

You can try the Controls.Find() method

        for (int k = 0; k < 50; i++)
        {
            Control[] arr = this.GroupBox1.Controls.Find("ComboBox" + k, true);
            if(arr.Length > 0)
            {
                ComboBox cb = arr[0] as ComboBox;
                Console.WriteLine(cb.SelectedIndex);
            }
        }
    }

Upvotes: 1

Related Questions