How to get a checked radio button in a groupbox?

前端 未结 11 1243
谎友^
谎友^ 2020-12-03 07:46

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

But I think

相关标签:
11条回答
  • 2020-12-03 08:34

    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
    
    0 讨论(0)
  • 2020-12-03 08:37

    try this

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

    this will return the Checked RadioButton in a GroupBox

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

    Imports System.Linq
    

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

    0 讨论(0)
  • 2020-12-03 08:37

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

    var checkedButton = GroupBox1.Controls.OfType<RadioButton>()
                                          .FirstOrDefault(rb => rb.Checked);
    
    0 讨论(0)
  • 2020-12-03 08:40

    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.

    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 讨论(0)
  • 2020-12-03 08:40
    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
    
    0 讨论(0)
提交回复
热议问题