
[英]C# - If A Radiobutton Selected In GroupBox, Unselect Other Radiobuttons In Other GroupBoxes
[英]Event handler for groupBox with radioButtons in C#
我在 groupBox 中有一些 radioonButtons,我需要做一些我可以称之为“其中一个 radiobuttons.checked 更改”的操作,或者从 radiobutton 中找出更改了什么索引。 我试图在事件列表中找到它,但找不到合适的。
编辑:为了更清楚:我需要知道是否存在一些handel,用于为goupBox而不是单个radioButton编写处理程序方法。 我知道如何使用 radiButton.checkedChanged,但这不是我所发现的。 我发现处理程序“在组框中发生了一些事情”或类似的(如果存在)。
它位于 Visual Studio 2012 的WFA(Windows 演示应用程序)中。
我认为您想要做的是将所有 RadioButtons 的 CheckedChanged 事件连接到同一个处理程序。
public Form1()
{
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
// ...
}
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton1.Checked)
{
// Do stuff
}
else if (radioButton2.Checked)
{
// Do other stuff
}
}
据我所知,没有内置任何内容。
将标签属性设置为某种指标(0 到 n)即可。
添加一个 CheckChangedHandler
将所有按钮 CheckChanged 事件指向它。
然后像。
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
int buttonid = (int)radioButton.Tag;
switch (buttonid)
{
case 0 : // do something; break
}
}
如果您有其中的一些,我会查看 radiogroup 组件。
我遇到了同样的问题:一个名为Button Type (gbxButtonType)的组框有 6 个单选按钮,另一个名为Icon Type (gbxIconType) 的组框有 8 个单选按钮。 当用户从每个组框中选择一个单选按钮时,单击DisplayButton后将出现一个 MessageBox 并应用该选择。 我的问题是组框没有 CheckedChanged 事件。 AKN的解决方案完美运行:
public Form1()
{
InitializeComponent();
for (int i = 0; i < gbxButtonType.Controls.Count; i++)
{
RadioButton rdb = (RadioButton)gbxButtonType.Controls[i];
rdb.CheckedChanged += new System.EventHandler(gbxButtonType_CheckedChanged);
}
for (int i = 0; i < gbxIconType.Controls.Count; i++)
{
RadioButton rdb = (RadioButton)gbxIconType.Controls[i];
rdb.CheckedChanged += new System.EventHandler(gbxIconType_CheckedChanged);
}
}
private void gbxIconType_CheckedChanged(object sender, EventArgs e)
{
if (sender == rdbAsterisk)
{
iconType = MessageBoxIcon.Asterisk;
}
else if (sender == rdbError)
{
iconType = MessageBoxIcon.Error;
}
...
else
{
iconType = MessageBoxIcon.Warning;
}
}
类似于 davenewza 的回答(可能应该是评论,但我没有足够的声誉),但是对于整个单选按钮组,该事件仅触发一次。
public Form1()
{
// Add a "CheckedChanged" event handler for each radio button.
// Ensure that all radio buttons are in the same groupbox control.
radioButton1.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(radioButtons_CheckedChanged);
}
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
// Do stuff only if the radio button is checked (or the action will run twice).
if (((RadioButton)sender).Checked)
{
if (((RadioButton)sender) == radioButton1)
{
// Do stuff
}
else if (((RadioButton)sender) == radioButton2)
{
// Do other stuff
}
}
}
System.Windows.Forms.RadioButton.CheckedChanged
是你需要的活动
因此,请执行以下操作:
public Form1()
{
InitializeComponent();
this.radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// your action
}
Groupbox 将限制只选中一个单选按钮
所以Setp1:您可以为所有单选按钮分配一个“CheckedChanged”事件处理程序
private void initRadio()
{
radio_button1.CheckedChanged += Radio_show_CheckedChanged;
radio_button2.CheckedChanged +=Radio_show_CheckedChanged;
}
和Setp2:像这样实现这个事件处理程序(按单选按钮的文本过滤)
private void Radio_show_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = sender as RadioButton;
if (radioButton.Checked == true) { //limited only checked button do function
switch (radioButton.Text)
{
case "name1":
// do your stuff ...
break;
case "name2":
// do your stuff ...
break;
}
}
}
我认为您想使用 groupbox 控件本身来处理 groupbox 内某些单选按钮的选择。
可能您希望这基本上是为了避免代码重复。
(即)为设计器中的所有单选按钮添加检查更改事件,当有更多控制时可能会很乏味。 既然它已经存在于一个组下,为什么不使用组控制对象来操作其中的控件并设置事件。
这就是我理解您的问题的方式,因此解决方案如下所示。
为分组框中的所有单选按钮控件设置一个通用处理程序
for (int i = 0; i < groupBox.Controls.Count; i++)
{
RadioButton rb = (RadioButton)groupBox.Controls[i];
rb.CheckedChanged += new System.EventHandler(evntHandler);
}
在处理程序中,您可以根据其他人的指示确定哪个按钮已更改并执行必要的操作。
//这是乔克·弗兰克·哈利迪的礼貌
//^subscribe events to radio button check changed
private void seriesTxtBxEvent()
{
//Show txtBx
this.radBtn_RoomSeries.CheckedChanged += new EventHandler(showSeriesTxtBx_Event);
//Hide txtBx
this.radBtn_RoomNumber.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
this.radBtn_RoomName.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
this.radBtn_RoomLevel.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
this.radBtn_RoomDep.CheckedChanged += new EventHandler(hideSeriesTxtBx_Event);
}
private void hideSeriesTxtBx_Event(object sender, EventArgs e)
{
tbx_SheetSeries.Visible = false;
}
private void showSeriesTxtBx_Event(object sender, EventArgs e)
{
tbx_SheetSeries.Visible = true;
}
//Form Start
void MainFormLoad(object sender, EventArgs e)
{
Control.ControlCollection locais = groupBoxLocalização.Controls;
foreach (CheckBox chkBox in locais)
{
chkBox.MouseUp += chkBoxLocais_MouseUp;
}
}
// Event
void chkBoxLocais_MouseUp(object sender, MouseEventArgs e)
{
//Tratar individualmente
CheckBox chk = (CheckBox)sender;
//ou para tratar todos objetos de uma vez
Control.ControlCollection locais = groupBoxLocalização.Controls;
foreach (CheckBox chkBox in locais) {
//chkBox....
}
}
您也许可以使用 Timer 来实现,但这对于优化来说是不利的,简单的解决方案是对于每个单选按钮,您只需添加一个函数作为 ChekedChanged 事件。
代码:
private void radioButtons_CheckedChanged (object sender, EventArgs e)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked == false) return;
// Do your thing
}
在设计器事件列表中的一个单选按钮上创建事件 Checked_Changed。
从每个单选按钮的 Checked_Changed 事件前面的下拉列表中向每个单选按钮添加相同的事件
内部检查更改事件使用
private void CheckedChanged(object sender,EventArgs e) { var radio = groupBox.Controls.OfType<RadioButton> ().FirstOrDefault(r => r.Checked).Name; }
您可以了解现在哪个收音机处于活动状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.