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

2023-09-02 01:48:53 作者:荒芜了青春又负了谁/*

我有很多的单选按钮的组框。通常,我会检查每个单选按钮单独使用如果radiobutton1.Checked = TRUE,则

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 maybe there is smart way to check which radio button being checked in a groupbox. Any idea?

推荐答案

试试这个

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

这将返回经过单选分组框

请注意,这是一个LINQ查询,而且必须有

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

Imports System.Linq

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

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