Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在groupbox中获得选中的单选按钮?_.net_Vb.net_Winforms_Radio Button_Groupbox - Fatal编程技术网

.net 如何在groupbox中获得选中的单选按钮?

.net 如何在groupbox中获得选中的单选按钮?,.net,vb.net,winforms,radio-button,groupbox,.net,Vb.net,Winforms,Radio Button,Groupbox,我在群组框中有很多单选按钮。通常,我会使用单独检查每个单选按钮,如果radiobutton1.Checked=True,则使用 但我认为也许有一种聪明的方法可以检查在分组框中选中了哪个单选按钮。有什么想法吗?如果将它们(例如加载事件)添加到列表中,您可以使用LINQ: Dim checkedRadioButton as RadioButton checkedRadioButton = radioButtonList.FirstOrDefault(Function(radioButton

我在群组框中有很多单选按钮。通常,我会使用
单独检查每个单选按钮,如果radiobutton1.Checked=True,则使用

但我认为也许有一种聪明的方法可以检查在分组框中选中了哪个单选按钮。有什么想法吗?

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

Dim checkedRadioButton as RadioButton
checkedRadioButton = 
    radioButtonList.FirstOrDefault(Function(radioButton) radioButton.Checked))
这应该可以,因为最多只检查一个

编辑 更好:只需查询GroupBox的控件集合:

Dim checkedRadioButton as RadioButton
checkedRadioButton = 
    groupBox.Controls.OfType(Of RadioButton)().FirstOrDefault(Function(radioButton) radioButton.Checked))
请注意,如果Groupbox中没有单选按钮,这将导致问题

试试这个

Dim rButton As RadioButton = 
        GroupBox1.Controls
       .OfType(Of RadioButton)
       .FirstOrDefault(Function(r) r.Checked = True)
这将在
分组框中返回选中的
单选按钮

请注意,这是一个LINQ查询,您必须

Imports System.Linq

如果您不这样做,IDE/编译器可能会指示类型为
不是
System.Windows.Forms.Control.ControlCollection

的成员。下面是一个带有四个单选按钮的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

您可以运行foreach循环来迭代控件的扫描 GroupBox中的RadioButton,因此使用该控件,请参见下面的示例

    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

再见

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

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

我有一个简单的

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

有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

我从表中的项目名称创建了三个单选按钮。我还为此创建了一个事件处理程序。现在,当我试图通过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

我认为在vb.net中,您需要使用
控件,而不是
儿童
,这是正确的。不仅在VB中,在C#中也是如此;它是一个属性名。我与WPF/Silverlight混在一起了,'IsChecked'不是'RadioButton'的成员,
其中不需要
,因为您也可以将Lambda表达式传递给
FirstOrDefault()
方法。这是一个很好的解决方案。您可以通过将.Where替换为.SingleOrDefault并删除.FirstOrDefault.This使它变得更简单。这使我免于编写一个巨大的if语句其他答案更简洁,但这是一个不需要控件名称的蛮力方法。这是一个非常蛮力的尝试,与大if-elseif差异最小,然后阻止OP试图避免的操作。这是否回答了您的问题?
'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
Select Case True
    RadioButton1.checked
        'Do this action for Rad1
    RadioButton2.checked
        'Do this action for Rad2
    RadioButton3.checked
        'Do this action for rad3
End Select
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
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