Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I am using 3 checkboxes in my application and i want to select one checkbox at a time.please tell me how to do it..
i am using VB.NET 08 and SQL server 05...
Posted
Comments
Abhijit Parab 24-Sep-12 1:30am    
What type of application, you are working on? Desktop application or Web Aplication?
[no name] 24-Sep-12 1:33am    
it's a desktop application...

Group the different choice sets in separate group boxes (or panels, or other container controls, but group boxes are probably what you're after).
Refer How to: Group Windows Forms RadioButton Controls to Function as a Set[^]
Or you can try this:
C#
//Assign same checked change event for all the CheckBoxes.
private List<CheckBox> _CheckBoxGroup = new List<CheckBox>();
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)sender;
    if (chk.Checked)
    {
        foreach(CheckBox other in _CheckBoxGroup)
        {
            if (other == chk)
            {
                continue;
            }
            other.Checked = false;
        }
    }
}



--Amit
 
Share this answer
 
v3
hi,

check these links.

CheckedListBox Class[^]
CheckedListBox.SelectionMode Property [^]

hope this helps.
 
Share this answer
 
Comments
[no name] 24-Sep-12 1:49am    
can u plz tell me for separate check boxes.because i m using that only..
Hi,

see this VB.NET code:
VB
Private Sub checkBox_CheckedChanged(sender As Object, e As EventArgs)
    If DirectCast(sender, CheckBox).Checked = False Then
        Return
    End If

    For Each ctrl As Control In DirectCast(sender, CheckBox).Parent.Controls
        If TypeOf ctrl Is CheckBox AndAlso ctrl <> sender Then
            DirectCast(ctrl, CheckBox).Checked = False
        End If
    Next
End Sub

You just need to add event handlers for your three checkboxes to the above code which is similar to the solution of Amy, but works without having a collection of checkboxes.
Use this for adding the event handlers:
VB
Me.checkBox1.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
Me.checkBox2.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
Me.checkBox3.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
 
Share this answer
 
Comments
[no name] 24-Sep-12 3:18am    
please tell me how to use the below part of ur code...
JF2015 24-Sep-12 3:28am    
For example you can add that to the constructor of your form as here:
Public Sub New()
InitializeComponent()
Me.checkBox1.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
Me.checkBox2.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
Me.checkBox3.CheckedChanged += New System.EventHandler(Me.checkBox_CheckedChanged)
End Sub
[no name] 24-Sep-12 3:29am    
ok ok..thanks..
JF2015 24-Sep-12 3:31am    
Please try it out and upvote/accept my answer. Thanks!
[no name] 24-Sep-12 3:38am    
i did this but it's showing that u have to raiseevent statement to raise the events..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900