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_famil开发者_如何学Gole 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 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;
精彩评论