[英]Grouping Windows Forms Radiobuttons with different parent controls in C#
我有一个Windows窗体应用程序,其中我有许多RadioButtons。 这些RadioButton放置在FlowLayoutPanel中 ,它自动为我安排它们。 直接添加到FlowLayoutPanel的所有RadioButton都被分组,这意味着我只能选择其中一个。 但是,其中一些RadioButtons与TextBox配对,所以我可以在那里提供一些参数。 但是为了正确安排所有这些,我将一个Panel控件添加到FlowLayoutPanel,这样我就可以自己控制RadioButton和TextBox相对于彼此的对齐。
这些RadioButton现在有各自的Panel作为父控件,因此不再包含在与其他RadioButtons的无线电组中。 我读到System.Web.UI命名空间中的RadioButtons具有GroupName属性,但不幸的是他们的System.Windows.Forms对应物缺少此属性。 有没有其他方法我可以组合这些单选按钮是我将不得不自己处理onClick事件?
谢谢,杰里
我担心你必须手动处理它......实际上并没有那么糟糕,你可以将所有RadioButton存储在一个列表中,并为所有这些使用单个事件处理程序:
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;
}
}
}
我同意@JonH - 使用标签是最干净的方法(imho)
private void FormLoad(object sender, EventArgs e)
{
radioCsv.Tag = DataTargetTypes.CsvFile;
radioTabbed.Tag = DataTargetTypes.TxtFile;
radioSas.Tag = DataTargetTypes.SasFile;
}
private void RadioButtonCheckedChanged(object sender, EventArgs e)
{
var radio = (RadioButton) sender;
this.DataDestinationType = (DataTargetTypes)radio.Tag;
}
这是对第一个答案的一点改进:创建一个封装分组功能的RadioGroup类,并添加对标准键盘导航(向上/向下键)的支持,并使标签工作。
要使用它,只需在表单中声明一个RadioGroup成员并将其新建(在InitializeComponent()之后),按正确的顺序传递组中所需的所有单选按钮。
public class RadioGroup
{
List<RadioButton> _radioButtons;
public RadioGroup(params RadioButton[] radioButtons)
{
_radioButtons = new List<RadioButton>(radioButtons);
foreach (RadioButton radioButton in _radioButtons)
{
radioButton.TabStop = false;
radioButton.KeyUp += new KeyEventHandler(radioButton_KeyUp);
radioButton.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
_radioButtons[0].TabStop = true;
}
void radioButton_KeyUp(object sender, KeyEventArgs e)
{
e.Handled = true;
RadioButton radioButton = (RadioButton)sender;
int index = _radioButtons.IndexOf(radioButton);
if (e.KeyCode == Keys.Down)
{
index++;
if (index >= _radioButtons.Count)
{
index = 0;
}
e.Handled = true;
}
else if (e.KeyCode == Keys.Up)
{
index--;
if (index < 0)
{
index = _radioButtons.Count - 1;
}
e.Handled = true;
}
radioButton = _radioButtons[index];
radioButton.Focus();
radioButton.Select();
}
void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton currentRadioButton = (RadioButton)sender;
if (currentRadioButton.Checked)
{
foreach (RadioButton radioButton in _radioButtons)
{
if (!radioButton.Equals(currentRadioButton))
{
radioButton.Checked = false;
}
}
}
}
}
需要注意的是:向上/向下键不适用于现有的RadioButton类,因为它已经处理了向上/向下键。 一种简单的方法可以将其修复为子类RadioButton并关闭向上/向下键的处理:
public class RadioButtonEx : RadioButton
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Up || keyData == Keys.Down)
{
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
@Jerry,我对Windows Forms不太熟悉,但我会拍摄一下。 如果有一个名为Tag的属性,您可以使用唯一标记标记每个单选按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.