为什么事件:在form_load事件中触发comboBox SelectedIndexChanged?

时间:2011-08-22 12:05:51

标签: c# events form-load

当我加载包含组合框的表单时,我注意到触发了comboBox_SelectedIndexChanged事件,但我不想在表单加载时使用该事件,您打算如何避免此问题。

我尝试通过将Boolean设置为false来停止它,并告诉comboBox_SelectedIndexChanged仅在布尔值等于true时才执行。当然,在form_load事件结束后,我将布尔值设置为true,但这不起作用。

任何帮助将不胜感激

这是代码:

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
                {
                    setprice(comboBox1, textBox1, textBox2, 0);


                }

 public void setprice(ComboBox combo, TextBox prix, TextBox qt, int num)
            {
                if (flag1[num] == 1)
                {
                    SqlDataReader reader = null;
                    try
                    {
                        SqlCommand command = mySqlConnection1.CreateCommand();
                        command.CommandText = "select prix_vente_ttc, prix_vente_ht from STK_PRODUITS_GENERIQUE where num_produit=" + combo.SelectedValue.ToString();
                        reader = command.ExecuteReader();
                        while (reader.Read())
                        {

                            prix_ttc[num] = Convert.ToDouble(reader.GetDecimal(0));
                            prix_ht[num] = Convert.ToDouble(reader.GetDecimal(1));
                            prix_htt[num] = prix_ht[num] * Convert.ToInt16(qt.Text);
                            //fact.forfait.setPrix_ht( prix_htt);
                            //fact.forfait.setTva(prix_ttct - prix_htt);

                            prix_ttct[num] = prix_ttc[num] * Convert.ToDouble(qt.Text);
                            prix.Text = Convert.ToString(prix_ttct[num]);
                        }
                        //textBox3_TextChanged(null, null);
                        // reader.Close();


                    }
                    catch (Exception excep)
                    {
                        MessageBox.Show(excep.Message); 
                        //if (reader != null) reader.Close();
                    }
                    if (reader != null) reader.Close();


                }
                flag1[num] = 1;
            }

private void vidangeform_Load(object sender, EventArgs e)
        {
            flag1[0] = 0;
            flag1[1] = 0;
            SqlDataReader reader = null;
            try
            {
                nextform = new filtreform();


                SqlCommand command = mySqlConnection1.CreateCommand();
                command.CommandText = "select designation, num_produit  from STK_PRODUITS_GENERIQUE where STK_PRODUITS_GENERIQUE.num_famille in (select num_famille from parametrage_vidange where parametrage_vidange.produit='forfait_vidange')";

                Dictionary<int, string> dict = new Dictionary<int, String>();
                reader = command.ExecuteReader();

                while (reader.Read())
                {

                    dict.Add(reader.GetInt32(1), reader.GetString(0));

                }
                comboBox1.DataSource = new BindingSource(dict, null);
                comboBox1.DisplayMember = "Value";
                comboBox1.ValueMember = "Key";
                reader.Close();

                command = mySqlConnection1.CreateCommand();
                command.CommandText = "select designation, num_produit  from STK_PRODUITS_GENERIQUE where STK_PRODUITS_GENERIQUE.num_famille in (select num_famille from parametrage_vidange where parametrage_vidange.produit='huile')";

                dict = new Dictionary<int, String>();
                reader = command.ExecuteReader();

                while (reader.Read())
                {

                    dict.Add(reader.GetInt32(1), reader.GetString(0));

                }
                comboBox2.DataSource = new BindingSource(dict, null);
                comboBox2.DisplayMember = "Value";
                comboBox2.ValueMember = "Key";
                reader.Close();
            }
            catch (Exception ep) { MessageBox.Show("problème de connexion avec le serveur ou resultat retourné nul. \n" + ep.Message); if(reader != null) reader.Close(); }



        }

1 个答案:

答案 0 :(得分:0)

为comboBox设置DataSource时会触发SelectedIndexChanged。

很难想出一种使用WinForms管理事件的简洁方法,但你可以做的不是在设计时订阅事件,你可以在设置dataSource后订阅它:

  • 在设计时从组合框事件中删除事件处理程序comboBox1_SelectedIndexChanged
  • 在vidangeform_Load方法的末尾添加以下行:

    comboBox1.SelectedIndexChanged + = comboBox1_SelectedIndexChanged;