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

发布于 2024-12-01 04:24:39 字数 4153 浏览 0 评论 0原文

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

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



        }

When i load a form that contains a combobox, i noticed that the comboBox_SelectedIndexChanged event is triggered, but i don't want to use that event at form load, what do you propose to avoid this problem.

I tried to stop it by setting a Boolean to false, and told to the comboBox_SelectedIndexChanged to execute only if the boolean equals true. of course after the form_load event finishes, I set the boolean to true, but that's not working.

Any help would be appreciated

here is the code :

 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(); }



        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心 2024-12-08 04:24:39

当您为组合框设置数据源时,将触发 SelectedIndexChanged。

很难想出一种使用 WinForms 管理事件的干净方法,但是您可以做的是,您可以在设置数据源之后订阅它,而不是在设计时订阅事件:

  • 从设计时的组合框事件
  • 在 vidangeform_Load 方法的末尾添加以下行:

    comboBox1.SelectedIndexChanged +=comboBox1_SelectedIndexChanged;

SelectedIndexChanged is fired when you set the DataSource for the comboBox.

It is hard to come up with a clean way of managing events with WinForms, but what you could do is instead of subscribing to the event at design-time, you could subscribe to it after you set the dataSource:

  • remove the event handler comboBox1_SelectedIndexChanged from your combobox events in design time
  • add the following line at the end of your vidangeform_Load method:

    comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文