Answered by:
Getting selected Radiobutton

Question
-
I've been doing .NET for a couple of months now, but still feel like a complete noob every now and then...
I have a couple of radiobuttons that are all placed in the same panel, and hence belong to the same group. If one is checked, the others are automatically unchecked.
How do I get the .text from whichever one is checked? I could do "If radiobutton1.checked = true then...end if" and then continue through each and every radiobutton in the group, but that is rather cumbersome.
So...is there a more efficient way so that I could do something like "Textbox1.text = radiobuttongroup1.selectedbutton.text"?
- Edited by Ricky Blunda Tuesday, July 15, 2014 10:35 AM
Tuesday, July 15, 2014 10:05 AM
Answers
-
Public Class Form1 Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged For Each obj In GroupBox1.Controls If TypeOf obj Is RadioButton Then Dim chk = DirectCast(obj, RadioButton) If chk.Checked = True Then Label1.Text = chk.Name End If End If Next End Sub End Class
Success
Cor- Marked as answer by Ricky Blunda Tuesday, July 15, 2014 2:03 PM
Tuesday, July 15, 2014 12:54 PM -
Hello,
The following is setup as follows, Panel1 contains RadioButton1 and RadioButton2 (more can be added), double click RadioButton1 and CheckedChanged event is created, add in the handles clause , RadioButton2.CheckedChanged then add the code below.
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _ Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged TextBox1.Text = ( From T In Panel1.Controls.OfType(Of RadioButton)() Where T.Checked Select T.Text ).FirstOrDefault End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Cor Ligthert Tuesday, July 15, 2014 1:15 PM
- Marked as answer by Ricky Blunda Tuesday, July 15, 2014 2:03 PM
Tuesday, July 15, 2014 12:57 PM -
Perhaps even easier:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
dim chk = DirectCast(sender, RadioButton)If chk.Checked Then TextBox1.Text = chk.Text
End SubDavid M. Nichols software engineer
- Marked as answer by Ricky Blunda Wednesday, July 16, 2014 6:49 AM
Tuesday, July 15, 2014 4:19 PM -
Or using Lamda
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _ Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged TextBox1.Text = Panel1.Controls.OfType(Of RadioButton) _ .Where(Function(rb) rb.Checked).FirstOrDefault.Text End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Marked as answer by Ricky Blunda Wednesday, July 16, 2014 6:49 AM
Tuesday, July 15, 2014 4:33 PM
All replies
-
I've solved it temporarily by creating a textbox that will hold the value (.text) of whichever radiobutton is selected. I can then extract the text from that textbox. I.e.
---------------------------------------------------------------------------------------------
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
tempbox.Text = RadioButton1.Text
End If
End Sub---------------------------------------------------------------------------------------------
It works, but I still thing there is an easier and more flexible solution to be found somewhere :)
- Edited by Ricky Blunda Tuesday, July 15, 2014 11:36 AM
Tuesday, July 15, 2014 10:40 AM -
I don't know how many radio buttons you have, but you could create a single CheckedChange event handler and add it to all radio buttons, instead of using individual handlers for each button.Tuesday, July 15, 2014 11:20 AM
-
Public Class Form1 Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged For Each obj In GroupBox1.Controls If TypeOf obj Is RadioButton Then Dim chk = DirectCast(obj, RadioButton) If chk.Checked = True Then Label1.Text = chk.Name End If End If Next End Sub End Class
Success
Cor- Marked as answer by Ricky Blunda Tuesday, July 15, 2014 2:03 PM
Tuesday, July 15, 2014 12:54 PM -
Hello,
The following is setup as follows, Panel1 contains RadioButton1 and RadioButton2 (more can be added), double click RadioButton1 and CheckedChanged event is created, add in the handles clause , RadioButton2.CheckedChanged then add the code below.
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _ Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged TextBox1.Text = ( From T In Panel1.Controls.OfType(Of RadioButton)() Where T.Checked Select T.Text ).FirstOrDefault End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Cor Ligthert Tuesday, July 15, 2014 1:15 PM
- Marked as answer by Ricky Blunda Tuesday, July 15, 2014 2:03 PM
Tuesday, July 15, 2014 12:57 PM -
Ok, both work fabolously :) Many thanks!
Tuesday, July 15, 2014 2:04 PM -
Perhaps even easier:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
dim chk = DirectCast(sender, RadioButton)If chk.Checked Then TextBox1.Text = chk.Text
End SubDavid M. Nichols software engineer
- Marked as answer by Ricky Blunda Wednesday, July 16, 2014 6:49 AM
Tuesday, July 15, 2014 4:19 PM -
Or using Lamda
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) _ Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged TextBox1.Text = Panel1.Controls.OfType(Of RadioButton) _ .Where(Function(rb) rb.Checked).FirstOrDefault.Text End Sub
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Marked as answer by Ricky Blunda Wednesday, July 16, 2014 6:49 AM
Tuesday, July 15, 2014 4:33 PM