Dynamically created checkboxes within a panel - how do i get the value?

M

Mike Fellows

I have created some checkboxes within a panel using the code below


Dim NewCheckbox As New CheckBox
Me.Panel2.Controls.Add(NewCheckbox)
NewCheckbox.Location = New Point(XLocation, YLocation)
NewCheckbox.AutoSize = True
NewCheckbox.Text = DS1.Tables(0).Rows(counter1)(0)
NewCheckbox.Name = "Checkbox" & counter1 + 1

the problem I have is how do i access the value of that checkbox? (usually
it would be just checkbox1.checked, but obvioulsy this does not work)

Thanks

Mike Fellows
 
M

Mike Fellows

maybe you should read before posting

and atleast attempt to spell correctly

the checkbox names i gave where an example

and the for loop is fairly obvious, but again that was not my question!
 
G

Guest

Hi Mike,
Are the checkboxes the only controls in the panel? If so, then the
panels control collection would be 1-1 wth the checkboxes. ie CheckBox0 <=>
Me.Panel2.Controls(0). If not, why not keep a seperate collection of
references to the checkboxes you add and reference the correct one via the
index of the collection.
 
C

CaffieneRush

I think you are confusing the name of the checkbox and the reference to
the checkbox, you cannot just use the name of the checkbox to access
it's members as you suggest. Instead, you need the reference to the
checkbox.

So Asad was basically correct.

Dim cbx As checkbox
'Get a reference to the cbx named checkbox1 within panel1
For c as control in panel1.controls
If typeof(c) is checkbox then
cbx = c
If cbx.Name = "checkbox1" then
Exit For
Else
cbx = Nothing
End If
End If
Next

If Not cbx Is Nothing Then
If cbx.Checked then
'checkbox named checkbox1 is checked so do whatever here.
End if
End If

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top