Reputation: 303
I have 8 comboboxes on my form that will all hold the same values - Yes and No.
Is there a quicker way than having to do combox1.items.add("Yes") etc?
I have the following but I cant seem to find anything to do with adding the items.
Dim cmb As Control
For Each cmb In Panel1.Controls
If TypeOf cmb Is ComboBox Then
'cmb. isnt beinging anything up for adding items?
End If
Next
Cheers
Upvotes: 2
Views: 2785
Reputation: 53
This Code will be useful, if you didnt use panel. If you have 5 comboboxes give count 1 to 5 and name of those comboboxes like ComboBox1, ComboBox2, comboBox3 etc.
For count = 1 To 5
Dim combobox = DirectCast(Me.Controls("ComboBox" & n & ""), ComboBox)
combobox.Items.Add("Ok")
Next
Hope this code also helps you.
Upvotes: 0
Reputation: 26424
I would create a DataSource
containing {Yes,No}
values, for example as a List
and then just do this:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.DataSource = myYesNoDataSource
Next
Later if you need to accept Y
and N
in place of Yes
and No
, you can convert to Dictionary
and set ValueMember
and DisplayMember
accordingly. Plus your list of available values is only initialized once. So your solution becomes flexible.
Upvotes: 1
Reputation: 460108
You can use Enumerable.OfType
:
For Each cmb In Panel1.Controls.OfType(Of ComboBox)()
cmb.Items.Add("Yes")
Next
Upvotes: 4