Results 1 to 18 of 18

Thread: Make user select radio button

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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:

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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...
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    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

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Make user select radio button

    Quote Originally Posted by kebo View Post
    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%
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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?

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Make user select radio button

    Quote Originally Posted by stratos View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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:
    1. Dim J As Integer
    2.         Dim txt As New List(Of TextBox)
    3.  
    4.         If CheckBox1.Checked = False And CheckBox2.Checked = False Then
    5.             MsgBox("PICK UP SOMETHING! SELECT OPT1 OR OPT2", MsgBoxStyle.OkOnly, "WARNING!")
    6.             GoTo es
    7.         End If
    8.  
    9.         If CheckBox1.Checked = True Then
    10.             For Me.J = 0 To 1
    11.                 If Not IsNumeric(txt(J).Text) Then
    12.                     MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING")
    13.                     Call clrtxt(sender) ' that's a subroutine
    14.                     GoTo es
    15.                 End If
    16.             Next J
    17.         End If
    18.  
    19.         If CheckBox2.Checked = True Then
    20.             For Me.J = 0 To 2
    21.                 If Not IsNumeric(txt(J).Text) Then
    22.                     MsgBox("PLEASE ENTER ONLY NUMBERS AND FILL IN ALL NECCESSARY FIELDS!", MsgBoxStyle.Critical, "WARNING")
    23.                     Call clrtxt(sender) ' that's a subroutine
    24.                     GoTo es
    25.                 End If
    26.             Next J
    27.         End If
    28.  
    29.         If RadioButton1.Checked = True Then
    30.             Call f1(sender) 'that another subroutine
    31.         End If
    32.         If RadioButton2.Checked = True Then
    33.             Call f2(sender) 'that another subroutine
    34.         End If
    35.  
    36.         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 ;]
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    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

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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..:/

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Make user select radio button

    You forgot the [/code] at the end of your code....

    but take a look at my post #2
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  14. #14

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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'

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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)()
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  16. #16
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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?

  18. #18
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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
  •  



Click Here to Expand Forum to Full Width