2011-08-22 206 views
0

當我加載一個包含組合框的表單時,我注意到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(); } 



     } 

回答

0

當您設置組合框的數據源的SelectedIndexChanged被激發。

這是很難拿出來與WinForms的管理事件的清潔方式,但你可以做的是,而不是訂閱在設計時的事件,你可以訂閱它,你設置DataSource後:

  • 移除事件處理程序從在設計時的組合框的事件comboBox1_SelectedIndexChanged
  • 在你vidangeform_Load方法的末尾添加下列行:

    comboBox1.SelectedIndexChanged + = comboBox1_SelectedIndexChanged;

+0

謝謝,它的工作。 –

相關問題