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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6466952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:40:53  来源:igfitidea点击:

How to get a checked radio button in a groupbox?

.netvb.netwinformsvisual-studio-2010radio-button

提问by Predator

I have a lot of radio buttons in a groupbox. Normally I will check each radio button individually using If radiobutton1.Checked = True Then.

我在 groupbox 中有很多单选按钮。通常我会使用If radiobutton1.Checked = True Then.

But I think maybe there is smart way to check which radio button being checked in a groupbox. Any idea?

但我认为也许有一种聪明的方法来检查在 groupbox 中选中哪个单选按钮。任何的想法?

回答by Binil

try this

尝试这个

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

this will return the Checked RadioButtonin a GroupBox

这将返回 Checked RadioButtonin aGroupBox

Note that this is a LINQ query, and you must have

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

Imports System.Linq

If you do not, your IDE/Compiler may indicate that OfTypeis not a member of System.Windows.Forms.Control.ControlCollection

如果不这样做,您的 IDE/编译器可能会表明它OfType不是System.Windows.Forms.Control.ControlCollection

回答by Erno

If you add them (Load event for instance) to a List you could use LINQ:

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

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

This should be OK because there is a single one checked at the most.

这应该没问题,因为最多只检查一个。

EDITEven better: just query the Controls collection of the GroupBox:

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

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

Be aware that this will cause problems if there are no RadioButtons in the Groupbox!

请注意,如果 Groupbox 中没有 RadioButtons,这将导致问题!

回答by Amberlea Moore

'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

回答by Loofer

I know it is tagged vb.net but here is a c# example

我知道它被标记为 vb.net 但这里是 ac# 示例

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

回答by dbasnett

Here is a test program with a groupbox with four radio buttons.

这是一个带有带有四个单选按钮的分组框的测试程序。

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

回答by Carmelo La Monica

You can run a foreach loop to iterate with the scans of controls RadioButton inside the GroupBox, so using the Control, see example below.

您可以运行 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

Bye

再见

回答by Said Marar

I have simple one and easy

我有一个简单的

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

回答by SkyMaster

There are 3 RadioButtons: RadioButton1, RadioButton2 y RadioButton3

有 3 个单选按钮:RadioButton1、RadioButton2 y 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

回答by Deb

I have created three radio buttons from the item names from a table. I also create an event handler for that. Now when I try to get which button is checked by its text value in a msgbox.It shows the correct name but msgbox popup twice for a single choice.I am using VB.net 2012.

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

回答by Ali

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