Radio Buttion

vks.gautam1

Well-known member
Joined
Oct 10, 2008
Messages
78
Location
Chandigarh, India
Programming Experience
Beginner
im using 3Radio Buttons on a form. Wt i want if user doesn't select any of radio button.It will give message & exit from code.

VB.NET:
If Not Rb1.Checked Or Rb2.Checked Or Rb3.Checked Then
            MsgBox("Pls select One Cluster ")
            Exit Sub
        End If
is there any other way to exit from procedure other than "exit sub"
 
First up, that code is wrong. If you want to check whether none of the three is checked then you either need to use parentheses:
VB.NET:
If Not (Rb1.Checked Or Rb2.Checked Or Rb3.Checked) Then
or else use three Not operators and use And rather than Or:
VB.NET:
If Not Rb1.Checked And Not Rb2.Checked And Not Rb3.Checked) Then
Also, it is almost always preferable to use AndAlso rather than And and to use OrElse rather than Or.

As for the question, why do you need to exit at all? Just let the method complete normally:
VB.NET:
If Rb1.Checked OrElse Rb2.Checked OrElse Rb3.Checked Then
    'A selection has been made so do your thing here.
Else
    'No selection made so show a message.
End If
 
Um, the compiler is going to compile all the code regardless. Maybe you should look at fixing those errors rather than sticking in an Exit Sub and ignoring them. If you tell us what they are we can help you fix them.
 
Back
Top