C# 如何从分组框中选中哪个单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18547326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get which radio button is checked from a groupbox?
提问by Amit Bisht
I have these groupboxes:
我有这些分组框:
I want to run some code according to checked true state of a radio button like:
我想根据单选按钮的检查真实状态运行一些代码,例如:
string chk = radiobutton.nme; // Name of radio button whose checked is true
switch(chk)
{
case "Option1":
// Some code
break;
case "Option2":
// Some code
break;
case "Option3":
// Some code
break;
}
Is there any direct way so that I can only get the name of the checked radio button?
有没有什么直接的方法可以让我只能获得选中单选按钮的名称?
采纳答案by Soner G?nül
You can find all checked RadioButtons like
您可以找到所有选中的 RadioButtons,例如
var buttons = this.Controls.OfType<RadioButton>()
.FirstOrDefault(n => n.Checked);
Also take a look at CheckedChanged
event.
也看看 CheckedChanged
事件。
Occurs when the value of the Checked property changes.
当 Checked 属性的值更改时发生。
回答by King King
You should take some look at the CheckedChanged
event to register the corresponding event handler and store the Checked
radio button state in some variable. However, I would like to use LINQhere just because you have just some RadioButtons
which makes the cost of looping acceptable:
您应该查看CheckedChanged
事件以注册相应的事件处理程序并将Checked
单选按钮状态存储在某个变量中。但是,我想在这里使用LINQ只是因为您只有一些RadioButtons
使得循环成本可以接受:
var checkedRadio = new []{groupBox1, groupBox2}
.SelectMany(g=>g.Controls.OfType<RadioButton>()
.Where(r=>r.Checked))
// Print name
foreach(var c in checkedRadio)
System.Diagnostics.Debug.Print(c.Name);
回答by Masoud Sahabi
In my opinion, it's better if you use RadioGroup instead of GroupBox. If you use radioGroup, you can always find the selected item easily like this:
在我看来,最好使用 RadioGroup 而不是 GroupBox。如果您使用 radioGroup,您总是可以像这样轻松找到所选项目:
radioGroup.selectedIndex;
If you design using Windows Forms, I suggest to implement RadioGroup behavior like this (please note that my code is in Java):
如果您使用 Windows 窗体进行设计,我建议实现这样的 RadioGroup 行为(请注意,我的代码是用 Java 编写的):
for (Component comp:groupBox1.components) {
if (((RadioButton)comp).selected)
return ((RadioButton)comp).value;
}
You can put this code block in a method to return selected radioButton value, and then you can use this value in your SWITCH part.
您可以将此代码块放在一个方法中以返回选定的 radioButton 值,然后您可以在 SWITCH 部分中使用此值。
回答by Bede Amarasekara
Rather than checking all RadioButtons, use the Validated event of the GroupBox.
使用 GroupBox 的 Validated 事件,而不是检查所有 RadioButton。
private void grpBox_Validated(object sender, EventArgs e)
{
GroupBox g = sender as GroupBox;
var a = from RadioButton r in g.Controls where r.Checked == true select r.Name;
strchecked = a.First();
}
回答by Meet Vasani
groupbox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked).Name
this will get the name of checked radio button. If you want to use it later, you might store name of that by storing into variable.
这将获得选中单选按钮的名称。如果你想稍后使用它,你可以通过存储到变量来存储它的名称。
Cheers
干杯