Click here to Skip to main content
11,437,397 members (33,788 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 23-Sep-12 20:27pm
Code-Hunt7.1K
Comments
Abhijit Parab at 24-Sep-12 1:30am
   
What type of application, you are working on? Desktop application or Web Aplication?
snehasish nandy at 24-Sep-12 1:33am
   
it's a desktop application...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
//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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,

check these links.

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

hope this helps.
  Permalink  
Comments
snehasish nandy at 24-Sep-12 1:49am
   
can u plz tell me for separate check boxes.because i m using that only..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,

see this VB.NET code:
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:
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)
  Permalink  
Comments
snehasish nandy at 24-Sep-12 3:18am
   
please tell me how to use the below part of ur code...
JF2015 at 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
snehasish nandy at 24-Sep-12 3:29am
   
ok ok..thanks..
JF2015 at 24-Sep-12 3:31am
   
Please try it out and upvote/accept my answer. Thanks!
snehasish nandy at 24-Sep-12 3:38am
   
i did this but it's showing that u have to raiseevent statement to raise the events..
JF2015 at 24-Sep-12 3:46am
   
Normally RaiseEvent should occur when you click on a checkbox, you don't need to trigger it manually. Where does it state that?
snehasish nandy at 24-Sep-12 3:48am
   
the second part of your code when i pasted in my application that time it's giving..

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



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100