Heang S.
Heang S.

Reputation: 13

How to Create Event for Dynamic Control Array in VB.NET

I'm trying to create an array of checkboxes dynamically and also want to put event to those checkboxes. How can I do this?

For example:

I have a array of checkboxes - Chk1, Chk2.

I want it to work this way: When I check Chk1, I want to disable Chk2, and when Chk1 is unchecked, Chk2 is enable, and vice versa.

Your input is greately appreciated.

Thanks,

P.S.: The code is in VB.NET. Thanks.


Thank you all for the inputs. I really appreciated it. Maybe I wasn't very clear on my explanation earlier.

Let's say, I have an array of 6 checkboxes, and I want them to behave in group like this:

and so on....

So this is like each pair of checkboxes in the array perform the CheckChanged event upon each other, but not on any other pair. So I think OptionButton is not the case in this situation.

Thanks for any suggestions.

Upvotes: 1

Views: 14234

Answers (2)

xpda
xpda

Reputation: 15813

An RadioButton would do this automatically and is more conventional. For an array of checkboxes, you can use a single handler for the entire array:

Private Sub _CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Integer
If sender.checked Then
  For i = 0 To UBound(chk)
    If chk(i) IsNot sender Then chk(i).Checked = False
  Next i
End If
End Sub

Keep in mind, if you make changes to this handler, that it is called recursively when you set chk(i).checked to false. It doesn't matter in this case, because it skips everything when sender.checked is false.

In case you need it, here is one way to set up the array. (The index property in the designer disappeared in the upgrade from vb6 to .net, so you have to make an array of controls in the code now.)

Public Class Form1
Dim chk(4) As CheckBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, k As Integer
k = 20
For i = 0 To UBound(chk)
  chk(i) = New CheckBox
  Me.Controls.Add(chk(i))
  chk(i).Location = New Point(20, k)
  k = k + chk(0).Height * 1.5 ' or some location
  chk(i).Text = "Checkbox " & i ' some appropriate text
  AddHandler chk(i).CheckedChanged, AddressOf _CheckedChanged
Next i

End Sub

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460158

Assuming that it is ASP.Net, have a look at this "strange" example to see how it works(take your array instead of my static creation):

Private Sub WebForm1_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    For number As Int32 = 1 To 100
        Dim chk As New CheckBox
        chk.ID = "chk" & number
        chk.Text = chk.ID
        chk.AutoPostBack = True
        AddHandler chk.CheckedChanged, AddressOf onCheckedChanged
        Me.MyChkPanel.Controls.Add(chk)
    Next
End Sub

Private Sub onCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim chk As CheckBox = DirectCast(sender, CheckBox)
    Dim number As Int32 = Int32.Parse(chk.ID.Substring("chk".Length))
    Dim otherChk As CheckBox
    If number Mod 2 = 0 Then
        otherChk = DirectCast(Me.MyChkPanel.FindControl("chk" & (number - 1)), CheckBox)
    Else
        otherChk = DirectCast(Me.MyChkPanel.FindControl("chk" & (number + 1)), CheckBox)
    End If
    otherChk.Enabled = Not chk.Checked
End Sub

Apart from that i can subscribe Hans' suggestion to use RadioButtons or at least a CheckBoxList.

Upvotes: 1

Related Questions