如何在组合框中获取已选中的单选按钮?

问题描述 投票:16回答:11

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

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

.net vb.net winforms visual-studio-2010 radio-button
11个回答
45
投票

试试这个

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

这将返回RadioButton中的Checked GroupBox

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

Imports System.Linq

如果不这样做,您的IDE /编译器可能会指出OfType不是System.Windows.Forms.Control.ControlCollection的成员


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

-3
投票
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

7
投票

如果将它们(例如Load事件)添加到List,则可以使用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))

请注意,如果组框中没有单选按钮,这将导致问题!


6
投票
'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
投票

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

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

1
投票

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

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
投票

我很简单,很容易

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

0
投票

有3个RadioButton: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
© www.soinside.com 2019 - 2024. All rights reserved.