-
Jul 15th, 2013, 12:56 PM
#1
Thread Starter
Member
Make user select radio button
Hello guys!
I am trying to find a code to make user select a radio button to continue into the process
I have used a loop and an IF statement but I can't find a way to declare how to pickup a radio button
This is the code
For Each Me.I In Me.Controls
If TypeOf I Is RadioButton Then
If ............................ Then ' HERE IS MY PROBLEM. I WANT TO SAY THAT IS NOTHING IS SELECTED THEN END PROCCESS
MsgBox("PICK UP A RADIOBUTTON", MsgBoxStyle.OkOnly, "WARNING!")
GoTo es
End If
End If
Next I
es:
-
Jul 15th, 2013, 01:20 PM
#2
Re: Make user select radio button
What I typically do is automatically select a radiobutton, either in design or in code. That away a radio button will always be checked. To do it in code, just place it in your form_load:
Code:
RadioButton1.Checked = True
If you don't want that(for w/e reason) check for the checked property and use a Boolean value:
Code:
'With LINQ:
Dim IsChecked As Boolean = False
For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton)()
If rb.Checked = True Then
IsChecked = True
End If
Next
If IsChecked Then
'There is atleast one radiobutton checked
Else
'There is not atleast one radiobutton checked
End If
Code:
'Without LINQ:
Dim IsChecked As Boolean = False
For Each rb As RadioButton In Me.Controls
If TypeOf rb Is RadioButton Then
If rb.Checked = True Then
IsChecked = True
End If
End If
Next
If IsChecked Then
'There is atleast one radiobutton checked
Else
'There is not atleast one radiobutton checked
End If
Also, you're using some visual basic 6 holdovers such as MsgBox and GoTo. MsgBox should be replaced with MessageBox and if you're using GoTo then you should defiantly try to restructure your code...
-
Jul 15th, 2013, 01:31 PM
#3
Re: Make user select radio button
the way I have done it is to disable the button that allows the user to continue in the form load event handler, then in the RadioButton.Checked event handler, enable the button. I have always discouraged allowing the user to click a button only to get a message that say they have to do something else first. If they shouldn't be allowed to do something, then simply don't let them
Last edited by kebo; Jul 15th, 2013 at 01:53 PM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jul 15th, 2013, 01:35 PM
#4
Re: Make user select radio button
Originally Posted by kebo
the way I have done it is to disable the button that allows the user to continue in the form load event handler, then in the radio.Checked event handler, enable the button. I have always discouraged allowing the user to click a button only to get a message that say they have to do something else first. If they shouldn't be allowed to do something, then simply don't let them
I agree 100%
-
Jul 15th, 2013, 01:58 PM
#5
Re: Make user select radio button
I'd want to do both, ideally. After all, the user may not realize what they haven't done, so just having a permanently disabled button is going to cause massive frustration unless there is something that will let them know that they have to take action, and what action they have to take. The best solution is to include a piece of hardware with a couple arms that spraypaints a stencil of the words "pick a radio button" on the users face, but those get expensive. The good thing about them is that nobody makes the mistake twice.
A more practical solution might be to add a messagebox, but the problem is when to add it. If the button is disabled, you can't add it in the button click. If there is no other alternative, I feel that you have to leave the button enabled, because the frustration of not being able to do what you need to do without any hint as to why, could easily be worse than the inconvenience of a messagebox. Furthermore, the button click event may be the only reaonable place to perform a check on the radio buttons, so you may just have to allow the click to happen.
My usual boring signature: Nothing
-
Jul 15th, 2013, 02:31 PM
#6
Thread Starter
Member
Re: Make user select radio button
Thanks for the replies guys. I have used the simpliest soluton. I have just forced a radiobutton to be checked from the beginning.
Also, I haven't thought some things you have mentioned, so I will rewrite some of the code to make the program more friendly to the user.
P.S. @dday9. What can I write to replace the 'GOTO' statement? I have used an EXIT SUB command, since I have made the 'GOTO' skip the code and go to the last line. I think it's the same thing.
What if I would like to go to another line?
-
Jul 15th, 2013, 02:35 PM
#7
Re: Make user select radio button
Could you post how your whole code goes?
Edit - I take that back, just the whole code for that Sub.
Last edited by dday9; Jul 15th, 2013 at 02:39 PM.
-
Jul 15th, 2013, 02:47 PM
#8
Thread Starter
Member
Re: Make user select radio button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim J As Integer
Dim txt As New List(Of TextBox)
If CheckBox1.Checked = False And CheckBox2.Checked = False Then
MsgBox("PICK UP SOMETHING! SELECT OPT1 OR OPT2", MsgBoxStyle.OkOnly, "WARNING!")
GoTo es
End If
If CheckBox1.Checked = True Then
For Me.J = 0 To 1
If Not IsNumeric(txt(J).Text) Then
MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING")
Call clrtxt(sender) ' that's a subroutine
GoTo es
End If
Next J
End If
If CheckBox2.Checked = True Then
For Me.J = 0 To 2
If Not IsNumeric(txt(J).Text) Then
MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING")
Call clrtxt(sender) ' that's a subroutine
GoTo es
End If
Next J
End If
If RadioButton1.Checked = True Then
Call f1(sender) 'that another subroutine
End If
If RadioButton2.Checked = True Then
Call f2(sender) 'that another subroutine
End If
es:
End Sub
-
Jul 15th, 2013, 02:48 PM
#9
Re: Make user select radio button
Originally Posted by stratos
Thanks for the replies guys. I have used the simpliest soluton. I have just forced a radiobutton to be checked from the beginning.
Also, I haven't thought some things you have mentioned, so I will rewrite some of the code to make the program more friendly to the user.
P.S. @dday9. What can I write to replace the 'GOTO' statement? I have used an EXIT SUB command, since I have made the 'GOTO' skip the code and go to the last line. I think it's the same thing.
What if I would like to go to another line?
Eww! Ick! No!...
try this:
Code:
dim bOKToContinue as Boolean = False
For Each Me.I In Me.Controls
If TypeOf I Is RadioButton Then
if I.Checked Then 'Or what ever it is for a RB, I'm shooting from the hip here....
bOKToContinue = True
Exit For
end if
Next I
if not bOKToContinue Then
MsgBox("PICK UP A RADIOBUTTON", MsgBoxStyle.OkOnly, "WARNING!")
end if
-tg
-
Jul 15th, 2013, 02:57 PM
#10
Re: Make user select radio button
Please be sure that next time you post code, you post it in code or highlight tags. To do so, you simply type {code}{/code} or {highlight="vb.net"}{/highlight} and just replace the curly brackets {} with square ones []. This is your code formatted correctly:
Code:
Dim J As Integer
Dim txt As New List(Of TextBox)
If CheckBox1.Checked = False And CheckBox2.Checked = False Then
MsgBox("PICK UP SOMETHING! SELECT OPT1 OR OPT2", MsgBoxStyle.OkOnly, "WARNING!")
GoTo es
End If
If CheckBox1.Checked = True Then
For Me.J = 0 To 1
If Not IsNumeric(txt(J).Text) Then
MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING")
Call clrtxt(sender) ' that's a subroutine
GoTo es
End If
Next J
End If
If CheckBox2.Checked = True Then
For Me.J = 0 To 2
If Not IsNumeric(txt(J).Text) Then
MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING")
Call clrtxt(sender) ' that's a subroutine
GoTo es
End If
Next J
End If
If RadioButton1.Checked = True Then
Call f1(sender) 'that another subroutine
End If
If RadioButton2.Checked = True Then
Call f2(sender) 'that another subroutine
End If
es:
vb.net Code:
Dim J As Integer Dim txt As New List(Of TextBox) If CheckBox1.Checked = False And CheckBox2.Checked = False Then MsgBox("PICK UP SOMETHING! SELECT OPT1 OR OPT2", MsgBoxStyle.OkOnly, "WARNING!") GoTo es End If If CheckBox1.Checked = True Then For Me.J = 0 To 1 If Not IsNumeric(txt(J).Text) Then MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING") Call clrtxt(sender) ' that's a subroutine GoTo es End If Next J End If If CheckBox2.Checked = True Then For Me.J = 0 To 2 If Not IsNumeric(txt(J).Text) Then MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING") Call clrtxt(sender) ' that's a subroutine GoTo es End If Next J End If If RadioButton1.Checked = True Then Call f1(sender) 'that another subroutine End If If RadioButton2.Checked = True Then Call f2(sender) 'that another subroutine End If es:
Something I notice is that you're using TextBoxes whenever numericupdowns should be used. NUDs store a numeric value and you get that value from numericupdown.value. But I did a refresh and it looks like TG beat me to the refined code, so use that ;]
-
Jul 15th, 2013, 03:06 PM
#11
Re: Make user select radio button
Here is another idea where there are two RadioButton controls directly on the form
Code:
' Has two methods for selection where on this form there are two RadioButton controls
Public Class Form1
Private SelectedRadioButton As RadioButton = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case True
Case RadioButton1.Checked
SelectedRadioButton = RadioButton1
Case RadioButton2.Checked
SelectedRadioButton = RadioButton2
End Select
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Selected = (From T In Me.Controls.OfType(Of RadioButton)() Where T.Checked).FirstOrDefault
If Selected IsNot Nothing Then
SelectedRadioButton = Selected
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If SelectedRadioButton Is Nothing Then
MessageBox.Show("Please make a selection")
e.Cancel = True
Else
MessageBox.Show("Selection is " & SelectedRadioButton.Name)
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
RadioButton1.Checked = False
RadioButton2.Checked = False
End Sub
End Class
Here the RadioButtons are in a GroupBox
Code:
' Has two methods for selection where on this form there are two RadioButton controls
Public Class Form1
Private SelectedRadioButton As RadioButton = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case True
Case RadioButton1.Checked
SelectedRadioButton = RadioButton1
Case RadioButton2.Checked
SelectedRadioButton = RadioButton2
End Select
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Selected = (From T In GroupBox1.Controls.OfType(Of RadioButton)() Where T.Checked).FirstOrDefault
If Selected IsNot Nothing Then
SelectedRadioButton = Selected
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If SelectedRadioButton Is Nothing Then
MessageBox.Show("Please make a selection")
e.Cancel = True
Else
MessageBox.Show("Selection is " & SelectedRadioButton.Name)
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
RadioButton1.Checked = False
RadioButton2.Checked = False
End Sub
End Class
-
Jul 15th, 2013, 03:08 PM
#12
Thread Starter
Member
Re: Make user select radio button
Yup, I forgot to place the code for the textboxes. I have added manually if the fisrt lien of the code i posted
Code:
txt.Add(TextBox1)
txt.Add(TextBox2)
txt.Add(TextBox3)
@ techgnome .I think something is wrong with your code. The I.CHECKED doesn't seem to work or something is missing..:/
-
Jul 15th, 2013, 03:10 PM
#13
Re: Make user select radio button
You forgot the [/code] at the end of your code....
but take a look at my post #2
-
Jul 15th, 2013, 03:21 PM
#14
Thread Starter
Member
Re: Make user select radio button
@dday9. One silly question. How have you dim rb? Is is a control. I still get an error 'rb.checked is not a memebr of the system.windows.form.control'
-
Jul 15th, 2013, 03:29 PM
#15
Re: Make user select radio button
I declared it in my for each loop:
Code:
For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton)()
-
Jul 15th, 2013, 03:32 PM
#16
Re: Make user select radio button
Yes, I know there's a problem with the code... I broke it intentionally to prevent blind copying/pasting...
-tg
-
Jul 15th, 2013, 03:35 PM
#17
Thread Starter
Member
Re: Make user select radio button
OK. Got it. My fault. Worked
What about the goto statement? I tried the exit for, but it just skiped the loop and go to the next line. Is there something I can use to go to any line I like?
-
Jul 15th, 2013, 04:15 PM
#18
Re: Make user select radio button
Here's the deal, windows form applications are event driven. Take this real-life example:
1) I go to the casino - Not done
2) I walk to the entrance - Not done
3) The bouncer checks my ID - Not done
4-a) If I'm over 21, then I go on the boat to gamble - Done
4-b) If I'm not 21 then I'm politely asked to leave - Not done
5-a) I leave - Done
5-b) I yell at the bouncer - Not done
6) I get thrown out for yelling at the bouncer - Done
What you're trying to do is:
1) I go to the casino
2) I go on the boat to gamble
what happens if the bouncer finds you on the boat gambeling and you're not 21 yet? You see where some errors can occur. That's why proper conditional statements constructs a good program flow.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|