English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i am using 2 check boxes..one is 'yes' and the other is 'no'....i need a code where u can only choose one or the other...not both of them at the same time...please help..10 points

2007-10-01 08:58:53 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

6 answers

'Try the following code:
'Use copy-paste.

Private Sub Check1_Click()
If Check1.Value = 0 Then: Check2.Value = 1: Else: Check2.Value = 0
End Sub

Private Sub Check2_Click()
If Check2.Value = 0 Then: Check1.Value = 1: Else: Check1.Value = 0
End Sub

'You can use radio buttons instead of check boxes so that you don't have to write any codes.

2007-10-04 00:11:00 · answer #1 · answered by ශාකුන්තල | shaakunthala 3 · 0 0

The following code will only allow a single check to be set at a time. Upon entering the click event of a check box a sub routine is called which unchecks all check boxes. A module level variable m_LastChecked is used to track the progress of clearing checkboxes and checking the current checkbox. This is important as it prevents multiple triggering of the click events. With out this variable and the if statments multiple triggering will repeat endlessly ultimately causing a stack overflow error halting the program.



Private m_LastChecked As Integer

Public Enum ChkBx
Unchecked = 0
Checked = 1
Greyed = 2
End Enum

Private Sub ClearChecks()
'Reset all check boxes to unchecked
If m_LastChecked < 0 Then Exit Sub ' test for -1 to prevent multi triggering of click event
Me.Check1.Value = ChkBx.Unchecked
Me.Check2.Value = ChkBx.Unchecked
Me.Check3.Value = ChkBx.Unchecked
m_LastChecked = -1 'Indicate that checks have been cleared
End Sub

Private Sub Check1_Click()
If m_LastChecked = 1 Then Exit Sub 'prevent multi triggering
ClearChecks
Me.Check1.Value = ChkBx.Checked
m_LastChecked = 1
End Sub


Private Sub Check2_Click()
If m_LastChecked = 2 Then Exit Sub 'prevent multi triggering
ClearChecks
Me.Check2.Value = ChkBx.Checked
m_LastChecked = 2
End Sub

Private Sub Check3_Click()
If m_LastChecked = 3 Then Exit Sub 'prevent multi triggering
ClearChecks
Me.Check3.Value = ChkBx.Checked
m_LastChecked = 3
End Sub

Private Sub Form_Load()
m_LastChecked = 0
ClearChecks
End Sub

2007-10-01 13:18:42 · answer #2 · answered by MarkG 7 · 0 0

Visual Basic 6.0 allows you to create applications, Visual Studio is an entirely different product. As for a free download, the software is still under Microsoft's copyright which would make it infringement. Just buy a copy, I sure someone sells it.

2016-05-18 03:09:30 · answer #3 · answered by ? 3 · 0 0

normally if you want a one or the other answer, you want radio buttons, not check boxes. As the natural state of a check box is "true" - checked or "false" unchecked; where a set of radio buttons are more about an "either/or" statement.
Check out the MSDN site I've listed the top will tell you how to add radio buttons; but if you must have check boxes it also shows you how to limit your answers. Hope it helped.


http://msdn2.microsoft.com/en-us/library/aa194401(office.11).aspx

2007-10-01 09:05:34 · answer #4 · answered by E.Nygma 2 · 0 1

if you only want one to be able to be selected at a time, use radio buttons rather than checkboxes. of the radiobuttons in a radio group, only one can be selected

2007-10-01 10:02:46 · answer #5 · answered by sk8er0114 3 · 0 0

if check1.value = 1 then check2.value = 0
if check2.value = 1 then check1.value = 0

something like that value isn't boolean so I assume it either 1 for checked and 0 for not checked

2007-10-01 09:10:09 · answer #6 · answered by the man the myth the answerer 5 · 0 1

fedest.com, questions and answers