Hi guys, can you please help me out.
Scenario:
I have 14 panels in my form, the user then input a number in the textbox. Then this textbox will now determine how many panels will be enable. My formula would be, userinputtextbox/0.5=panels to be open. Example: 3/0.5=6.

So far this is my code:

for x = 1 to UserInputTextbox.text
CType(Me.Controls("P" & x.ToString ), Panel).Enabled = True
next

But I got this error:
Object reference not set to an instance of an object.
And it highlighted my Ctype code. What I am lacking?

PS. The names of my panels were P1, P2.... P14 =)

For x As Integer = 1 To CInt(UserInputTextbox.Text)
            For Each ctl As Control In Me.Controls '// Loop thru all Controls on Form.
                If TypeOf (ctl) Is Panel Then '// Locate Panels.
                    If ctl.Name = "P" & x.ToString Then
                        ctl.Enabled = True
                        Exit For
                    End If
                End If
            Next
        Next

Hi thank you for this code. Another approach would be:

For i = 1 to CInt(UserInputCombobox.text)
   CType(Me.GroupBox1.Controls("P" & i.ToString), Panel).Enabled = False
Next

This one is shorter for those guys out there. But both codes are doing great. Thank you again. =)

commented: kudos for that code sample.:) +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.