|
-
Aug 2nd, 2010, 01:03 PM
#1
Thread Starter
New Member
Checkbox Array
Please help, ugh
I recently converted from VB6.0 to .Net 2005 and quickly realized control arrays are no longer used. In my larger project I create 50 check boxes on form load and want to take action on a check changed event when a user selects one of the check boxes.
Can someone please let me know how to reference the checkboxes once they're created.
Here is s simplified version of my code
Thank you
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim CheckBox As New CheckBox()
CheckBox.Text = "Checkbox1"
CheckBox.Location = New Point(100, 50)
CheckBox.Size = New Size(95, 45)
Me.Controls.Add(CheckBox)
End Sub
-
Aug 2nd, 2010, 01:59 PM
#2
Re: Checkbox Array
you can use an array to reference your checkboxes:
vb Code:
dim checkboxes(49) as checkbox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
for x as integer = 0 to 49
checkboxes(x) = New CheckBox
checkboxes(x).Text = "Checkbox" & (x + 1).tostring
checkboxes(x).Location = 'etc
checkboxes(x).Size = 'etc
Me.Controls.Add(checkboxes(x))
next
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 2nd, 2010, 03:06 PM
#3
Re: Checkbox Array
It's true that you can store the checkboxes in an array, but since you still need to add code for handling the CheckedChanged event and you always get a reference to the sender object in an event this is actually not necassary.
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For i As Integer = 0 to 49
Dim checkBox As New CheckBox
'checkBox.Size = 'blah blah....
Me.Controls.Add(checkBox)
AddHandler checkBox.CheckedChanged, AddressOf CheckBox_CheckedChanged
Next
End Sub
Private Sub CheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim checkBox As CheckBox = CType(sender, CheckBox)
'Now you have a reference to the checkbox that was just checked.
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|