如何在GroupBox中获取已选中的单选按钮?

17

我有很多单选按钮在一个组框中。通常我会使用If radiobutton1.Checked = True Then逐个检查每个单选按钮。

但是我认为可能有一种更加智能的方式来检查哪个单选按钮在组框中被选中。有什么想法吗?

注:单选按钮用英文"radio button"表示,组框用英文"groupbox"表示。


2
这回答解决了你的问题吗?如何确定单选按钮组中哪个被选中? - Gary Barrett
11个回答

46

试一试这个

Dim rButton As RadioButton = 
        GroupBox1.Controls
       .OfType(Of RadioButton)
       .FirstOrDefault(Function(r) r.Checked = True)

这将返回 GroupBox 中被选中的 RadioButton

请注意,这是一个LINQ查询,并且您必须要有:

Imports System.Linq
如果不这样做,你的 IDE/编译器可能会指出 System.Windows.Forms.Control.ControlCollection 中不存在 OfType 成员。

3
你可以将Lambda表达式传递给FirstOrDefault()方法,因此Where并不是必需的。 - Emond
4
这是一个很好的解决方案。你可以通过用.SingleOrDefault替换.Where并删除.FirstOrDefault来使它更简单。 - Ben Gripka
1
这刚刚让我免于编写一大段if语句。 - exceptional exception

8

如果您将它们(例如Load事件)添加到列表中,您可以使用LINQ:

Dim checkedRadioButton as RadioButton
checkedRadioButton = 
    radioButtonList.FirstOrDefault(Function(radioButton) radioButton.Checked))

这应该是可以的,因为最多只有一个被选中。

编辑 更好的方法是:只需查询GroupBox的Controls集合:

Dim checkedRadioButton as RadioButton
checkedRadioButton = 
    groupBox.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(radioButton) radioButton.Checked))

请注意,如果GroupBox中没有RadioButton,则会出现问题!

我认为在VB.NET中,你需要使用Controls而不是Children - Binil
你是正确的。不仅在VB中,在C#中也是一个属性名称。我把它和WPF / Silverlight混淆了。 - Emond
'IsChecked' 不是 'RadioButton' 的成员。 - htm11h

7
'returns which radio button is selected within GroupBox passed
Private Function WhatRadioIsSelected(ByVal grp As GroupBox) As String
    Dim rbtn As RadioButton
    Dim rbtnName As String = String.Empty
    Try
        Dim ctl As Control
        For Each ctl In grp.Controls
            If TypeOf ctl Is RadioButton Then
                rbtn = DirectCast(ctl, RadioButton)
                If rbtn.Checked Then
                    rbtnName = rbtn.Name
                    Exit For
                End If
            End If
        Next
    Catch ex As Exception
        Dim stackframe As New Diagnostics.StackFrame(1)
        Throw New Exception("An error occurred in routine, '" & stackframe.GetMethod.ReflectedType.Name & "." & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'." & Environment.NewLine & "  Message was: '" & ex.Message & "'")
    End Try
    Return rbtnName
End Function

2
其他答案更简洁,但这是一种不需要控件名称的暴力方法,很有趣。 - Hannele

2

我知道它标记为vb.net,但这里有一个C#示例:

var checkedButton = GroupBox1.Controls.OfType<RadioButton>()
                                      .FirstOrDefault(rb => rb.Checked);

1
这是一个带有四个单选按钮的GroupBox的测试程序。
Public Class Form1

    Private Sub Form1_Shown(sender As Object, _
                            e As System.EventArgs) Handles Me.Shown
        RadioButton1.Tag = New Action(AddressOf rb1Action)
        RadioButton2.Tag = New Action(AddressOf rb2Action)
        RadioButton3.Tag = New Action(AddressOf rb3Action)
        RadioButton4.Tag = New Action(AddressOf rb4Action)
    End Sub

    Private Sub rb1Action()
        Debug.WriteLine("1 " & RadioButton1.Checked)
    End Sub

    Private Sub rb2Action()
        Debug.WriteLine("2 " & RadioButton2.Checked)
    End Sub

    Private Sub rb3Action()
        Debug.WriteLine("3 " & RadioButton3.Checked)
    End Sub

    Private Sub rb4Action()
        Debug.WriteLine("4 " & RadioButton4.Checked)
    End Sub

    Private Sub RadioButton_CheckedChanged(sender As System.Object, _
                                            e As System.EventArgs) Handles _
                                        RadioButton1.CheckedChanged, _
                                        RadioButton2.CheckedChanged, _
                                        RadioButton3.CheckedChanged, _
                                        RadioButton4.CheckedChanged

        Dim aRadioButton As RadioButton = DirectCast(sender, RadioButton)
        If aRadioButton.Checked Then
            Dim rbAct As Action = DirectCast(aRadioButton.Tag, Action)
            rbAct.Invoke()
        End If
    End Sub
End Class

0
你可以使用foreach循环来迭代GroupBox内RadioButton控件的扫描,因此使用Control,参见下面的示例。
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'RadioButton checked
    Dim ceckedRadioButton As Integer = 0
    'Total RadioButton on GroupBox
    Dim totalRadioButton As Integer = 0

    'Iteration and check RadioButton selected
    For Each myControl As RadioButton In Me.GroupBox1.Controls.OfType(Of RadioButton)()
        'If RadioButton is checked
        If myControl.Checked Then
            'increases variable ceckedRadioButton
            ceckedRadioButton += 1
        End If

        'increases variable totalRadioButton
        totalRadioButton += 1
    Next

    If ceckedRadioButton > 0 Then
        'RadioButon show how many are selected
        MessageBox.Show("Were selected" & " " & ceckedRadioButton.ToString & " " & "RadioButton on" & " " & totalRadioButton.ToString)
    Else
        'No selected RadioButton
        MessageBox.Show("No selected RadioButton")
    End If
End Sub

再见


0
Private Sub BTN_OK_Click(sender As Object, e As EventArgs) Handles BTN_OK.Click

    For Each Ctrl In GroupBox1.Controls
        If Ctrl.checked Then MsgBox(Ctrl.text) 'for show select RadioButton Check
    Next

0

有3个单选按钮:RadioButton1、RadioButton2和RadioButton3

'A handler for the click event of the 3 buttons is created
Private Sub Radios_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
    Dim rb As RadioButton
    rb = sender
    MsgBox(rb.Name) 'Displays the name of the selected control or that he was made the click
End Sub

0

我从一个表格的项目名称中创建了三个单选按钮。我还为此创建了事件处理程序。现在,当我尝试通过其文本值获取哪个按钮被选中并在msgbox中显示时,它会显示正确的名称,但对于单个选择,msgbox会弹出两次。我正在使用VB.net 2012。

Private Sub iButton_checked(ByVal sender As System.Object, ByVal e As System.EventArgs)
    For Each b As RadioButton In grpgodown.Controls.OfType(Of RadioButton)()
        If b.Checked = True Then
            MsgBox(b.Text)
        End If
    Next
End Sub

0

我有一个简单而容易的。

For Each b As RadioButton In GroupBox1.Controls.OfType(Of RadioButton)()
        If b.Checked = True Then
            MsgBox("I hope that will help you")
        End If
    Next

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接