Hi everyone!
I have several combo-boxes that are supposed to contain information about different aspects of vehicles. Sometimes the combo-boxes may only have 1 element in them. I've been asked to set the default value of the combo-box to that element if there is only 1 in there. My question is how can I prevent an event from being skipped to set a value of a combo-box to that one element.
I have one vehicle that has 3 combo-boxes that all have 1 element in them. This code fires the 1st and the 3rd one, but skips the 2nd one. Can someone shed some light as to what I'm missing? I'm not getting any errors, it's just not filling in the engine-combobox in when there is one element in that combobox. It fills in the year and the transmissionCode ones just fine.
private void modelComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
yearComboBox.Enabled = true;
engineCodeComboBox.Enabled = false;
transmissionCodeComboBox.Enabled = false;
okButton.Enabled = false;
yearComboBox.Text = "";
engineCodeComboBox.Text = "";
transmissionCodeComboBox.Text = "";
yearComboBox.Items.Clear();
engineCodeComboBox.Items.Clear();
transmissionCodeComboBox.Items.Clear();
if (modelComboBox.SelectedIndex == -1)
{
yearComboBox.Enabled = false;
engineCodeComboBox.Enabled = false;
transmissionCodeComboBox.Enabled = false;
}
SqlCeCommand year_comm = new SqlCeCommand("SELECT DISTINCT year FROM vehicles WHERE make = @make AND model = @model ORDER BY year DESC", conn);
year_comm.CommandType = CommandType.Text;
year_comm.Parameters.AddWithValue("@make", makeComboBox.Text);
year_comm.Parameters.AddWithValue("@model", modelComboBox.Text);
year_comm.ExecuteNonQuery();
try
{
SqlCeDataReader year_dr = year_comm.ExecuteReader();
while (year_dr.Read())
{
yearComboBox.Items.Add(year_dr["year"]);
}
if (yearComboBox.Items.Count == 1)
{
yearComboBox.SelectedIndex = 0;
}
year_dr.Close();
year_dr.Dispose();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
year_comm.Dispose();
}
}
private void yearComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
engineCodeComboBox.Enabled = true;
transmissionCodeComboBox.Enabled = true;
okButton.Enabled = false;
engineCodeComboBox.Text = "";
transmissionCodeComboBox.Text = "";
engineCodeComboBox.Items.Clear();
transmissionCodeComboBox.Items.Clear();
if (yearComboBox.SelectedIndex == -1)
{
engineCodeComboBox.Enabled = false;
transmissionCodeComboBox.Enabled = false;
}
SqlCeCommand engine_comm = new SqlCeCommand("SELECT DISTINCT engine_code FROM vehicles WHERE make = @make AND model = @model AND year = @year", conn);
engine_comm.CommandType = CommandType.Text;
engine_comm.Parameters.AddWithValue("@make", makeComboBox.Text);
engine_comm.Parameters.AddWithValue("@model", modelComboBox.Text);
engine_comm.Parameters.AddWithValue("@year", yearComboBox.Text);
engine_comm.ExecuteNonQuery();
try
{
SqlCeDataReader engine_dr = engine_comm.ExecuteReader();
while (engine_dr.Read())
{
engineCodeComboBox.Items.Add(engine_dr["engine_code"]);
}
if (engineCodeComboBox.Items.Count == 1)
{
engineCodeComboBox.SelectedIndex = 0;
}
engine_dr.Close();
engine_dr.Dispose();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
engine_comm.Dispose();
}
}
private void engineCodeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
transmissionCodeComboBox.Enabled = true;
okButton.Enabled = false;
engineCodeComboBox.Text = "";
transmissionCodeComboBox.Text = "";
transmissionCodeComboBox.Items.Clear();
SqlCeCommand trans_comm = new SqlCeCommand("SELECT DISTINCT transmission_code FROM vehicles WHERE make = @make AND model = @model AND year = @year AND engine_code = @engine_code", conn);
trans_comm.CommandType = CommandType.Text;
trans_comm.Parameters.AddWithValue("@make", makeComboBox.SelectedItem);
trans_comm.Parameters.AddWithValue("@model", modelComboBox.SelectedItem);
trans_comm.Parameters.AddWithValue("@year", yearComboBox.SelectedItem);
trans_comm.Parameters.AddWithValue("@engine_code", engineCodeComboBox.SelectedItem);
trans_comm.ExecuteNonQuery();
try
{
SqlCeDataReader trans_dr = trans_comm.ExecuteReader();
while (trans_dr.Read())
{
transmissionCodeComboBox.Items.Add(trans_dr["transmission_code"]);
}
if (transmissionCodeComboBox.Items.Count == 1)
{
transmissionCodeComboBox.SelectedIndex = 0;
}
trans_dr.Close();
trans_dr.Dispose();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
trans_comm.Dispose();
}
}