Question:
Check Box one at a time for visual Basic?
?
2010-04-20 07:44:27 UTC
I am working on visual Basic and i'm using check boxes but when i check 2 they are both checked, how can i do it so when i check 1, then when i go to check number 2, number 1 gets un checked please reply :)
Nine answers:
2010-04-20 07:58:26 UTC
You have to do it by code.



There will be a click event handler for each check box you place on your form.



In that click event handler you should do a simple code to check each of the other check boxes.



Say you named them Check1 thru Check8



In Check1's Checked event handler you would do something like this.



If Check1.checked = True then



Check2.Checked = FALSE

Check3.Checked = FALSE

Check4.Checked = FALSE

Check5.Checked = FALSE

Check6.Checked = FALSE

Check7.Checked = FALSE

Check8.Checked = FALSE



Endif



Then you cut and paste to Check2 and make a slight modification to make Check2 into Check 1 as follows:



In Check2's Checked event handler you would do something like this.



If Check2.checked = True then



Check1.Checked = FALSE

Check3.Checked = FALSE

Check4.Checked = FALSE

Check5.Checked = FALSE

Check6.Checked = FALSE

Check7.Checked = FALSE

Check8.Checked = FALSE



Endif



Cut and paste then modify for each check box. When you run the program each IF statement will be checked and if any of the boxes are checked and the If statement will be processed unchecking all of the others.
pastageek
2010-04-20 07:54:27 UTC
Radio buttons dragged onto a group box will do the trick without any extra code and all you then need to worry about is the event handle of each radio button. With check boxes you could easily put them in a list or array and let a while or for loop uncheck them except one if you so wished. You could make a method out of this and call it in each check boxes event handler.
Hal 9000
2010-04-21 01:12:06 UTC
I'm presuming VB.NET rather than VB6

..



Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged



CheckBox2.Checked = False

''Do other stuff...

End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged

CheckBox1.Checked = False

''Do other stuff...

End Sub
fugua
2016-12-13 14:58:43 UTC
Vb Checkbox
?
2016-06-02 12:16:40 UTC
you add this code to that function If (chkAdd.Checked = False) Then lblAddAns.Visible = False End If when you click the check box again then the label will hide
2010-04-20 07:48:07 UTC
Don't use checkboxes, use radio buttons.



- Add a group box to the form

- Add radio buttons to the group box



It defaults to only allow one to be selected.
Merx
2010-04-20 07:49:10 UTC
Shouldn't you use an option group for that? Option groups allow only one of the possible options to be selected and automatically deselects the other options.



What are you developing this in?
Jack
2010-04-20 18:15:26 UTC
useing frames and radio buttons are your best bet.



Private Sub Check2_Click()

Check1.Value = vbUnchecked

End Sub
2010-04-20 08:04:26 UTC
It seems you are using individual check box instead use GroupBox so you can have a one choice from the group try the tutorial in this link.

http://www.homeandlearn.co.uk/net/nets4p13.html

Hope it will help


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...