|
-
May 15th, 2008, 11:18 AM
#1
Thread Starter
Fanatic Member
[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
-
May 15th, 2008, 11:28 AM
#2
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
-
May 15th, 2008, 11:44 AM
#3
Lively Member
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?
-
May 15th, 2008, 11:48 AM
#4
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:
'This is a VB highlight tag
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.
-
May 15th, 2008, 03:26 PM
#5
Thread Starter
Fanatic Member
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
-
May 15th, 2008, 03:31 PM
#6
Re: [2005] Is there an easy way to determine which radio button in a groupbox is selected
 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:
'This is a VB highlight tag
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.
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
|