我想做一个 combobox 级联 比如combobox1和combobox2  combobox1的绑定数据写在 Load 里面 则combobox2 的绑定数据写在 combobox1的SelectedValueChanged事件里面是吗?但是每次加载的时候都会未实例化的 因为第一次combobox1 还没有绑定上他就执行combobox1的SelectedValueChanged事件了,具体怎么做啊  在线等待谢谢

解决方案 »

  1.   


    在Load完毕之后再开始监听事件
    private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Add("a");
                comboBox1.Items.Add("b");
                comboBox1.Items.Add("c");
                comboBox1.Items.Add("d");
                comboBox1.SelectedValueChanged += new EventHandler(comboBox1_SelectedValueChanged);
            }        void comboBox1_SelectedValueChanged(object sender, EventArgs e)
            {
                Random rand = new Random();
                comboBox2.Items.Clear();
                comboBox2.Items.Add(comboBox1.SelectedText + rand.Next());
                comboBox2.Items.Add(comboBox1.SelectedText + rand.Next());
                comboBox2.Items.Add(comboBox1.SelectedText + rand.Next());
                comboBox2.Items.Add(comboBox1.SelectedText + rand.Next());
            }good luck
      

  2.   

    是不是可以把监听放在第二个cmb事件中呢,先填充完数据再处理第三个?
      

  3.   

    combobox1的绑定代码写在load
    combobox2的绑定代码可以写在combobox2的下拉事件里, sql的where范围指向combobox的txt值