954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Selecting Multiple Panels to be enable

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 =)

kruxena
Newbie Poster
14 posts since Jan 2011
Reputation Points: 11
Solved Threads: 2
 
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
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

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. =)

kruxena
Newbie Poster
14 posts since Jan 2011
Reputation Points: 11
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: