How to get a checked radio button in a groupbox?

前端 未结 11 1291
谎友^
谎友^ 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:18

    If you add them (Load event for instance) to a List you could use 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.

    EDIT Even better: just query the Controls collection of the GroupBox:

    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!

提交回复
热议问题