mekkakelvin
mekkakelvin

Reputation: 11

How to loop through groupbox in vb.net

In my VB.NET form I have 50 groupboxes, each containing a combobox named volt. I used this code to add value to all the comboboxes:

For count = 1 To 50
    Dim volt = DirectCast(Me.Controls("volt" & count & ""), ComboBox)
    volt.Items.Add("what a code")
Next

but they were placed in different groupbox. When I rewrite it like this:

For count = 1 To 50
    Dim volt = DirectCast(groupbox1.Controls("volt" & count & ""), ComboBox)
    volt.Items.Add("what a code")
 Next

it works in in only groupbox1. How can I make affect the remaining groupboxes?

Upvotes: 0

Views: 2826

Answers (1)

LuckyLuke82
LuckyLuke82

Reputation: 606

Something like this might work for you (jmcilhinney suggestion actually):

 For Each ctl As Control In Me.Controls
     For Each cmb As Combobox In ctl.Controls.OfType(Of Combobox)()
        cmb.Text = "Volt"
     Next
 Next

But be careful with this - If your Groupboxes are in a container such as Panel or Split Container do a loop inside that and not In Me.Controls.

Upvotes: 2

Related Questions