新的winform窗口里,有一个ComboBox控件 、一个GroupBox群组,GroupBox群组里有 3 个 RadioButton。我在ComboBox里 添加了 3 项值 var1,var2,var3,我本要选择了ComboBox里的值后,就选定对应的RadioButton我的ComboBox_SelectedIndexChanged(object sender, EventArgs e) 事件里 写了如下代码来实现这个问题
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int x = cbox.SelectedIndex; // 1
groupBox1.Controls[x].Select(); // 2
}调试发现 程序老在 1 2 处 打转,死循环了。
请教我该如何才能实现我要的功能??
或者说 代码不该丢 SelectedIndexChanged 事件里??那又要丢哪个事件里呢??
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int x = cbox.SelectedIndex; // 1
groupBox1.Controls[x].Select(); // 2
}调试发现 程序老在 1 2 处 打转,死循环了。
请教我该如何才能实现我要的功能??
或者说 代码不该丢 SelectedIndexChanged 事件里??那又要丢哪个事件里呢??
解决方案 »
- XML文件在写操作完成后怎么关闭?
- com口编程(不需要通讯)
- 用Enterprise Library 4.0 访问 MySql 数据库
- 设计模式、极限编程等与实际开发如何结合
- 一个你应该曾经遇到过的问题?菜了求救!——————在线等,甚盼
- Bezier曲线技术难点?如何确定其终点的方向
- C#中如何调用windows程序(一次性给分20分)
- 给个解释吧
- 想系统的学习.net,各位高手请推荐一些好的学习方式和学习资料吧,谢谢了
- 在web编程中怎么与数据库连不上?有人出现过这样的问题妈?着急死了,如果不行我以后的工作就没发作了。
- web编程,C#,数据查询问题
- 怎么才能让c#程序脱离sqlServer也能访问数据库?
{
this.ComboBox.SelectedIndexChanged -= ComboBox_SelectedIndexChanged;
int x = cbox.SelectedIndex; // 1
groupBox1.Controls[x].Select(); // 2
this.ComboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButton rb = groupBox1.Controls["radioButton" + (comboBox1.SelectedIndex + 1)] as RadioButton;
if (rb != null)
rb.Checked = true;
}
这里要向 2 楼 那样写:
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int x = cbox.SelectedIndex;
RadioButton rb = (RadioButton)groupBox1.Controls[x];
rb.Checked = true;
}可是我这么改了 还是 死循环哦!!它就是不跳出方法来老是在 ComboBox_SelectedIndexChanged 方法里面死循环。
我就是做做练习就出这问题了,窗体里十分干净 1 个ComboBox 1 个GroupBox 3 个RadioButton。
using System;
...
namespace Test4_1
{
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent(); } private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
cbox.SelectedIndex = 0;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
cbox.SelectedIndex = 1;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
cbox.SelectedIndex = 2;
}
private void cbox_SelectedIndexChanged(object sender, EventArgs e)
{
int x = cbox.SelectedIndex;
RadioButton rb = groupBox1.Controls[x] as RadioButton;
rb.Checked = true;
}
}
}
那肯定会出现你说的这个情况啦……
你为RadioButton指定Checked属性,它也会触发CheckedChanged事件的,然后你又在CheckedChanged事件里又设置ComboBox的SelectedIndex,那不就循环了吗……
当然死循环了,你cbox触发选定事件,然后在这个事件里,你选择radioButton。事件得触发radioButton的事件,然后在radioButton的事件又选定cbox。
这个当然死循环就相当二个人,张三和李四。你给他们下命令:张三,有人叫你时你一定要叫李四。李四,有人叫你时你一定要叫张三。结果他们两个就一直在那里叫了。
那就判断一下。private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(cbox.SelectedIndex!=0)
{
cbox.SelectedIndex = 0;
}
}
//radio的其它两个一样要判断这里不写了。 private void cbox_SelectedIndexChanged(object sender, EventArgs e)
{
int x = cbox.SelectedIndex;
RadioButton rb = groupBox1.Controls[x] as RadioButton;
if(!rb.Checked) rb.Checked= true;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButton rb = groupBox1.Controls["radioButton" + (comboBox1.SelectedIndex + 1)] as RadioButton;
if (rb != null)
{
rb.CheckedChanged -= new EventHandler(radioButton_CheckedChanged);
rb.Checked = true;
rb.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
}
} private void radioButton_CheckedChanged(object sender, EventArgs e)//3个RadioButton都用同一个事件处理方法
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
{
comboBox1.SelectedIndexChanged -= new EventHandler(comboBox1_SelectedIndexChanged);
comboBox1.SelectedIndex = int.Parse(rb.Name.Replace("radioButton", "")) - 1;
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}
}