问题描述
我有一个Windows Forms应用程序,其中有许多RadioButtons.这些RadioButton放置在 FlowLayoutPanel 中,它会自动为我安排它们.直接添加到FlowLayoutPanel的所有RadioButton进行了分组,这意味着我只能选择其中之一.但是,其中一些RadioButton与TextBox配对使用,因此我可以在此处提供一些参数.但是要正确安排所有这些,我将一个Panel控件添加到FlowLayoutPanel中,这样我就可以控制RadioButton和TextBox彼此之间的相对对齐.
I've got a Windows Forms application in which I have a number of RadioButtons. These RadioButtons are placed within a FlowLayoutPanel which automatically arranges them for me. All RadioButtons that are directly added to the FlowLayoutPanel are grouped, meaning I can select only one of them. However, some of these RadioButtons are paired up with a TextBox so I can supply some argument there. But to have all this arranged properly, I add a Panel control to the FlowLayoutPanel so I can control the alignment of the RadioButton and TextBox relatively to each other myself.
这些单选按钮现在具有各自的面板作为父控件,因此不再与其他单选按钮一起包含在单选组中.我读到 System.Web.UI 命名空间中的RadioButtons具有GroupName属性,但是不幸的是,它们的 System.Windows.Forms 对应对象却缺少此属性.我还可以通过其他方法来对这些单选按钮进行分组吗?我必须自己处理onClick事件吗?
These RadioButtons now have their own respective Panels as parent controls and thus are no longer included in the radio group with the other RadioButtons. I read that the the RadioButtons that are in the System.Web.UI namespace have a GroupName property, but unfortunately their System.Windows.Forms counterparts lack this property. Is there some other way I can group these radio buttons are am I going to have to handle onClick events myself?
谢谢,杰瑞
推荐答案
恐怕您必须手动处理此问题……实际上还不错,您可以将所有RadioButton都存储在一个列表中,并对所有这些事件使用单个事件处理程序:
I'm afraid you'll have to handle this manually... It's not so bad actually, you can probably just store all the RadioButton in a list, and use a single event handler for all of them:
private List<RadioButton> _radioButtonGroup = new List<RadioButton>();
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
if (rb.Checked)
{
foreach(RadioButton other in _radioButtonGroup)
{
if (other == rb)
{
continue;
}
other.Checked = false;
}
}
}
这篇关于使用C#中的不同父控件将Windows Forms单选按钮分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!