开发者

Select Case on Radiobutton group is it possible?

开发者 https://www.devze.com 2023-01-09 18:39 出处:网络
I have say 4 asp.net radiobuttons which are groupe开发者_JAVA技巧d, I want to find out which one is checked.

I have say 4 asp.net radiobuttons which are groupe开发者_JAVA技巧d, I want to find out which one is checked.

What seems natural to do is:

Select Case RadioButton.Checked = True
        Case myRadioButton1
        Case myRadioButton2
        Case myRadioButton3
        Case Else

End Select

I just get a 'reference to a non-shared member reference' error. It's a shame because it seems such a clean way to do this test.. Is it possible??


Select Case True

  Case RadioButton1.Checked

  Case RadioButton2.Checked

  Case RadioButton3.Checked

  Case Else

End Select


No. The Checked property of class RadioButton is non-shared, so you cannot get its value unless you have an instance. You can do this using an ElseIf chain, but the cleanest solution IMO is to use a RadioButtonList instead, which will allow you to add items dynamically and read the selected item in one call, just like with a DropdownList.


You could use a RadioButtonList instead. You can only check for primitive data-types in select/case. With normal RadioButtons you could only check for a reference in the CheckedChanged Event when all CheckBoxes are handled there. For example:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler Me.RadioButton1.CheckedChanged, AddressOf CheckedChanged
        AddHandler Me.RadioButton2.CheckedChanged, AddressOf CheckedChanged
    End Sub

    Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       If sender Is Me.RadioButton1 Then

        ElseIf sender Is Me.RadioButton2 Then
            '........
        End If
    End Sub

But that was not your question anyway. Use a RadiobuttonList instead as i mentioned above or a If/else on checked-state because it is more natural.

0

精彩评论

暂无评论...
验证码 换一张
取 消