hi everyone

i am try to write a code for a checkbox that work like a radioButton dynamicaly in vb.net. i.e. if i have 5 check box and i try to check the first one other will become unchecked.
Second with same code if i increase the number of check box in the Form same code will work for that also.

i try this code but it not good ..............

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
CheckBox2.Checked = False
End If
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked = True Then
CheckBox1.Checked = False
End If
End Sub

Try this:

Add to form load event

    'tell each checkbox to use the same handler for the CheckedChange event
    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
        AddHandler cb.CheckedChanged, AddressOf CheckBox_CheckedChanged
    Next

Add this sub to the form class:

Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'create a checkbox that equals the one being checked.
    Dim TempCB As CheckBox = DirectCast(sender, CheckBox)
    If TempCB.Checked Then
        For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
            'uncheck all the other checkboxes
            If Not cb.Name = TempCB.Name Then
                cb.Checked = False
            End If
        Next
    End If
End Sub

This will do what you describe, only one checkbox at a time can be checked. This will work for however many you want.

This question has already been answered. Start a new discussion instead.