C# 在C中使用不同的父控件对Windows窗体单选按钮进行分组#
我有一个Windows窗体应用程序,其中有许多单选按钮。这些单选按钮放置在FlowLayoutPanel中,FlowLayoutPanel会自动为我排列它们。所有直接添加到FlowLayoutPanel的单选按钮都被分组,这意味着我只能选择其中一个。然而,其中一些单选按钮与文本框配对,因此我可以在这里提供一些参数。但是为了让所有这些都安排妥当,我在FlowLayoutPanel中添加了一个面板控件,这样我就可以控制RadioButton和TextBox相对彼此的对齐 这些单选按钮现在有各自的面板作为父控件,因此不再与其他单选按钮一起包含在单选组中。我了解到System.Web.UI命名空间中的单选按钮具有GroupName属性,但不幸的是,它们的System.Windows.Forms对应项缺少此属性。是否有其他方法可以将这些单选按钮分组?我是否必须自己处理onClick事件 谢谢,C# 在C中使用不同的父控件对Windows窗体单选按钮进行分组#,c#,winforms,radio-button,C#,Winforms,Radio Button,我有一个Windows窗体应用程序,其中有许多单选按钮。这些单选按钮放置在FlowLayoutPanel中,FlowLayoutPanel会自动为我排列它们。所有直接添加到FlowLayoutPanel的单选按钮都被分组,这意味着我只能选择其中一个。然而,其中一些单选按钮与文本框配对,因此我可以在这里提供一些参数。但是为了让所有这些都安排妥当,我在FlowLayoutPanel中添加了一个面板控件,这样我就可以控制RadioButton和TextBox相对彼此的对齐 这些单选按钮现在有各自的面板
Jerry,我对Windows窗体不太熟悉,但我会试一试。如果有一个名为Tag的属性,您可以用一个唯一的标记来标记每个单选按钮。恐怕您必须手动处理此问题。。。其实也没那么糟糕,您可能只需将所有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;
}
}
}
private List\u radioButtonGroup=new List();
私有void单选按钮\u CheckedChanged(对象发送方,事件参数e)
{
RadioButton rb=(RadioButton)发送器;
如果(rb.选中)
{
foreach(RadioButton组中的其他RadioButton)
{
如果(其他==rb)
{
继续;
}
其他。选中=错误;
}
}
}
我同意@JonH-使用标签是最干净的方法(imho)
与第一个答案相比,这里有一个小小的改进:创建一个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;
}
}
}
}
}
通常我们会使用分组框对单选按钮进行分组。我担心会是这样。不过这不应该是个问题。感谢您的帮助。修复:rb.Checked=false;=>其他。选中=错误@麦克斯比,我不明白你的评论。。。什么意思?
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;
}
}
}
}
}
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);
}
}