Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
C#
private void modelComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Sets year combobox to true and all others to false when
            // selected index changed event changes the existing value.
            yearComboBox.Enabled = true;
            engineCodeComboBox.Enabled = false;
            transmissionCodeComboBox.Enabled = false;
            okButton.Enabled = false;

            // Sets currently disabled comboboxes to have null values
            // and waits for further action from end-user.
            yearComboBox.Text = "";
            engineCodeComboBox.Text = "";
            transmissionCodeComboBox.Text = "";

            // Clears out items in comboboxes and waits for a new filtered
            // query with results.
            yearComboBox.Items.Clear();
            engineCodeComboBox.Items.Clear();
            transmissionCodeComboBox.Items.Clear();

            if (modelComboBox.SelectedIndex == -1)
            {
                yearComboBox.Enabled = false;
                engineCodeComboBox.Enabled = false;
                transmissionCodeComboBox.Enabled = false;
            }

            // Query to select non-repeating years (filtering on selected make and model)
            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())
                {
                    // Fills year combobox with results from query
                    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();
            }
        }

        // Listener for yearcombobox selectedindexchanged event and loads the engine
        // combobox with engine type information
        private void yearComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Sets engine and transmission boxes to true.  OK button remains false until all 5 boxes have non null values.
            engineCodeComboBox.Enabled = true;
            transmissionCodeComboBox.Enabled = true;
            okButton.Enabled = false;

            // Should the selected index value change, these 2 boxes would
            // be reset to null values and wait for further selections.
            engineCodeComboBox.Text = "";
            transmissionCodeComboBox.Text = "";

            // Clears / Resets items in boxes and waits for new selections.
            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();
            }
        }

        // Listener for enginecombobox selectedindexchanged event and loads the transmission
        // combobox with transmission type information
        private void engineCodeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            transmissionCodeComboBox.Enabled = true;
            okButton.Enabled = false;
            engineCodeComboBox.Text = "";
            transmissionCodeComboBox.Text = "";
            transmissionCodeComboBox.Items.Clear();

            // Query to select transmission type (based on previously selected make, model and year)
            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())
                {
                    // Fills trans combobox with results from query
                    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();
            }
        }
Posted
Updated 15-Oct-12 9:41am
v3
Comments
[no name] 15-Oct-12 14:49pm    
Okay... what does " not filling in the engine-combobox in when there is one element in that combobox" mean exactly? Does your yearComboBox_SelectedIndexChanged code run at all? Do you get results back from your query? What do you see happening when you step through your code?
joshrduncan2012 15-Oct-12 14:53pm    
What I meant to say is that I get results from all of the queries. If there is one element for that combobox, I want to set the SelectedIndex to 0. The SelectedIndex = 0 works for yearComboBox and transmissionCodeComboBox, but not engineCodeComboBox. EngineCodeCombox, however, does have a choice to select, though.
Sergey Alexandrovich Kryukov 15-Oct-12 15:40pm    
First of all, you need to tag the application type or UI library you use. WPF? Forms? ASP.NET? Silverlight? Metro? what?
--SA
joshrduncan2012 15-Oct-12 15:41pm    
Thanks, I added Forms and ADO.NET.
Sergey Alexandrovich Kryukov 15-Oct-12 17:19pm    
Thank you for tagging it. It's good for you and should always be done.
And now, I'm not sure what is that skipping. First thing to do is to put break points on the beginning of each even handler and see what it does under debugger. Is some are not invoked at all, set a break point on each += operators of all event instances in question -- what is some handlers are never actually added to the invocation lists? This is all pretty simple; and you always come to figure out what's wrong.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900