0

コンボボックスを含むフォームをロードすると、comboBox_SelectedIndexChangedイベントがトリガーされることに気付きましたが、フォームのロード時にそのイベントを使用したくないので、この問題を回避するために何を提案しますか。

ブール値をfalseに設定して停止しようとし、ブール値がtrueの場合にのみ実行するようにcomboBox_SelectedIndexChangedに指示しました。もちろん、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(); }



        }
4

1 に答える 1

0

SelectedIndexChanged は、comboBox の DataSource を設定すると発生します。

WinForms でイベントを管理するクリーンな方法を考え出すのは難しいですが、設計時にイベントをサブスクライブする代わりに、dataSource を設定した後にサブスクライブすることができます。

  • 設計時にコンボボックスイベントからイベントハンドラーのcomboBox1_SelectedIndexChangedを削除します
  • vidangeform_Load メソッドの最後に次の行を追加します。

    コンボボックス1.SelectedIndexChanged += コンボボックス1_SelectedIndexChanged;

于 2011-08-22T12:29:54.500 に答える