为什么事件:comboBox_SelectedIndexChanged在form_load事件中被触发?

问题描述:

当我加载一个包含组合框的表单时,我注意到comboBox_SelectedIndexChanged事件被触发,但我不想在表单加载时使用该事件,你建议如何避免这个问题。为什么事件:comboBox_SelectedIndexChanged在form_load事件中被触发?

我试图通过设置一个布尔值为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(); } 



     } 

当您设置组合框的数据源的SelectedIndexChanged被激发。

这是很难拿出来与WinForms的管理事件的清洁方式,但你可以做的是,而不是订阅在设计时的事件,你可以订阅它,你设置DataSource后:

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

    comboBox1.SelectedIndexChanged + = comboBox1_SelectedIndexChanged;

+0

谢谢,它的工作。 –