How to access programatically created controls?

RaistlinMajere

New member
Joined
Mar 4, 2008
Messages
3
Programming Experience
3-5
Hello,

I looked all over Microsoft and Google and cannot find the answer to this.

My problem is that i have a form created at Runtime that adds checkbox controls to the form programatically depending upon how many are needed.

Now, I have a button on that form that when clicked, it should pull whatever info is located for each of the checkbox.tag properties that are checked.

Problem is, how do i check to see if 1 of the checkbox controls are checked when i click the Button when the checkbox controls aren't created within the code of that button before runtime??

I can't compile saying

IF (Chkbox1.checked=TRUE)

because Chkbox1, 2, 3, 20 aren't created yet.

Thanks for any info on this one.
 
Well, you could, at runtime, when the checkboxes are created, add them to an array of check boxes. Then in your code you can loop through the array and check if they are checked.
 
If these checkboxes are added to their own container like a groupbox you can also utilize this Controls collection and loop it (without need to skip different type controls).
 
Ok,

I understand what both of you are talking about, but the user would check the boxes after the form is created. The form isn't created with the checkboxes checked at run time. They are all unchecked.

Thanks
 
Ok,

I understand what both of you are talking about, but the user would check the boxes after the form is created. The form isn't created with the checkboxes checked at run time. They are all unchecked.

Thanks

I fail to see the problem? When the button is clicked you would then iterate through the array, or the controls collection, and check which ones are checked.
 
Perfect!

Should think out of the box a bit more. THanks again for your help. Controls Collection worked great

For Each ctrl As Control In myForm.GroupBox1.Controls
If TypeOf ctrl Is CheckBox Then
If DirectCast(ctrl, CheckBox).Checked = True Then


End If
End If
Next
 
Back
Top