新的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 事件里??那又要丢哪个事件里呢??

解决方案 »

  1.   

    粗略的修正可以这样做:private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) 

        this.ComboBox.SelectedIndexChanged -= ComboBox_SelectedIndexChanged;
        int x = cbox.SelectedIndex;        //  1 
        groupBox1.Controls[x].Select();    //  2 
        this.ComboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
      

  2.   


            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                RadioButton rb = groupBox1.Controls["radioButton" + (comboBox1.SelectedIndex + 1)] as RadioButton;
                if (rb != null)
                    rb.Checked = true;
            }
      

  3.   

    在SelectedIndexChanged这里没问题的.你其他地方可能有问题.贴窗体内的代码来瞧瞧
      

  4.   

    public event EventHandler SelectionChangeCommitted
      

  5.   

    1  2  楼 方法行不通哦!!!不过我知道了:  groupBox1.Controls[x].Select();  是错误的了
    这里要向 2 楼 那样写:
    private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                int x = cbox.SelectedIndex;
                RadioButton rb = (RadioButton)groupBox1.Controls[x];
                rb.Checked = true;
            }
    可是我这么改了 还是 死循环哦!!它就是不跳出方法来老是在 ComboBox_SelectedIndexChanged 方法里面死循环。
      

  6.   

    我还是把全部代码都贴了吧:
    我就是做做练习就出这问题了,窗体里十分干净 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;
            }
        }
    }
      

  7.   


    那肯定会出现你说的这个情况啦……
    你为RadioButton指定Checked属性,它也会触发CheckedChanged事件的,然后你又在CheckedChanged事件里又设置ComboBox的SelectedIndex,那不就循环了吗……
      

  8.   


    当然死循环了,你cbox触发选定事件,然后在这个事件里,你选择radioButton。事件得触发radioButton的事件,然后在radioButton的事件又选定cbox。
    这个当然死循环就相当二个人,张三和李四。你给他们下命令:张三,有人叫你时你一定要叫李四。李四,有人叫你时你一定要叫张三。结果他们两个就一直在那里叫了。
      

  9.   

    如果你的需求仅仅是:选择cbx以后会自动选择对应的rbx,那么为什么要在radio的checkedChanged里又添加代码呢?
      

  10.   

    如果你要不管选择的是Combox还是radio都相应地改变另一个的值。
    那就判断一下。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; 
            } 
      

  11.   


            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);
                }
            }