Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Is there an easy way to determine which radio button in a groupbox is selected

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Resolved [RESOLVED] [2005] Is there an easy way to determine which radio button in a groupbox is selected

    Currently I set a single form variable each time one of the radio buttons is selected but that seems a little clunky. Is there a better way?
    ManagePC - the all-in-one PC management and inventory tool

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Is there an easy way to determine which radio button in a groupbox is selected

    since radio buttons in .NET all need unique names, you just need to check and see if the given radio button is checked or not.

    You could write a generic routine to do this:
    Code:
        Private Function GetSelectedRadioButton(ByVal Container As Control) As RadioButton
            'loop all controls in container control
            For Each C As Control In Container.Controls
                'check to see if control is radio button
                If TypeOf C Is RadioButton Then
                    'cast control to radio button to see if its checked
                    Dim RB As RadioButton = DirectCast(C, RadioButton)
                    'if radio is checked return it
                    If RB.Checked Then Return RB
                End If
            Next
            'if we get here return nothing
            Return Nothing
        End Function
    usage would be something like this:
    Code:
            Dim RB As RadioButton = GetSelectedRadioButton(GroupBox1)
            If RB IsNot Nothing Then
                MessageBox.Show("Currently selected radio button is: " & RB.Name)
            Else
                MessageBox.Show("No radio button is currently selected")
            End If

  3. #3
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: [2005] Is there an easy way to determine which radio button in a groupbox is sele

    Hi!

    I allways go like this:
    Code:
       Private Sub RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
             Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, _
             RadioButton3.CheckedChanged, RadioButton4.CheckedChanged
          Dim Bt = DirectCast(sender, RadioButton)
          If Not Bt.Checked Then Return
          Select Case True
             Case Bt Is RadioButton1
                '...
             Case Bt Is RadioButton2
                '...
             Case Bt Is RadioButton3
                '...
             Case Bt Is RadioButton4
                '...
          End Select
       End Sub
    Question: How I get my snippets colored in this forum?

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Is there an easy way to determine which radio button in a groupbox is selected

    Nerd, the problem with that is it is totally hard coded. What happens when you add RadioButton5 to the form? Then you need to remember everyplace you have code like this, and need to update it to handle the new control. Using a more generic method removes the need to know the number of controls and their names.

    To color your code, instead of code tags you use highlight tags, and you set it to =vb

    Code:
    'This is a standard code tag
    Dim myString as String = "Hello World"
    vb Code:
    1. 'This is a VB highlight tag
    2. Dim myString as String = "Hello World"

    The reason I still like using the CODE tags better, is they copy/paste into VS nice and easy. The highlight tags use different HTML, so when you copy/paste into VS, you lose the line returns... so you generally need to first paste into WORDPAD, then copy/paste into VS, and it retains the carriage returns, however double spaces everything.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: [2005] Is there an easy way to determine which radio button in a groupbox is selected

    Simple and effective, just what I needed. Thanks Kleinma
    ManagePC - the all-in-one PC management and inventory tool

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Is there an easy way to determine which radio button in a groupbox is selected

    Quote Originally Posted by kleinma
    ...To color your code, instead of code tags you use highlight tags, and you set it to =vb

    Code:
    'This is a standard code tag
    Dim myString as String = "Hello World"
    vb Code:
    1. 'This is a VB highlight tag
    2. Dim myString as String = "Hello World"

    The reason I still like using the CODE tags better, is they copy/paste into VS nice and easy.
    AMEN!!!! I hate highlight tags for just that reason.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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