I have a Form that has 3 radio buttons grouped together. I have a default combobox selected on the form. When a user selects a item from the combo box a new combo box appears that is populated from the data in a access DB. Problem is that right now when you select a selection the new combo box does not appear. It instead appears when you select one of the other radio buttons then go back to the original ComboBox.

Here is the code.

First Part is the radio button which calls the next function

private void optProductColor_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (optProductColor.Checked == true)
                {              
                    this.lblProductColor.Visible = true;
                    this.cmbProductColor.Visible = true;
                    this.lblCustomerType.Visible = false;
                    this.cmbCustomerType.Visible = false;

                    GetProductColors();
                }
                    
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

Here is the next function

private void GetProductColors()
        {
            try
            {   //DB calls to Stored Procedure
                //Read the DB into our ComboBox
                while (oleDbDataReader1.Read() == true)
                {
                    this.cmbProductColor.Items.Add(oleDbDataReader1.GetString(0));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                oleDbConnection1.Close();
            }

            //Get Selection from Combo Box
            string strSelection = cmbProductColor.SelectedItem.ToString();
            //Make next combobox visible
            this.cmbFirstColor.Visible = true;
                
            //Send String to First Imprint Color
            GetFirstImprintColors(strSelection);
        }

This calls the next function which is not displayed until I leave the radio button and then come back

private void GetFirstImprintColors(string strSelection)
        {
            try
            {
                //See if DB is open
                //DB calls
                //Read the DB into our ComboBox
                while (oleDbDataReader1.Read() == true)
                {
                    this.cmbFirstColor.Items.Add(oleDbDataReader1.GetString(0));          
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                oleDbConnection1.Close();
            }

        }

How do I make each combo box visible after a selection is selected? Like a chain reaction?

Thanks

I have been trying to get this work and so far the only way i cna get this to work is if I use a If else statement that is dependent on the a user selection. But since I do not know the values of the combo box prior to the first entry I cannot assiga if else statement.

Is there any way to do a statment with a combo box on these osrt of lines?

if(combobox.selectedItem() = anything)
do this?

Since I do not know the argument prior to the selection I need the selected Item to be == to anything. Is there a way to do this?

Thanks

ok, in your GetProductsColor() you are populating the combo cmbProductColor - right? I would suggest doing cmbProductColor.Items.Clear(); before adding new stuff or you will append the values without getting rid of old values.

Later in the same function you are capturing selected item, well, the selected item is null because your user did not have a chance to actually seleect anything as of yet. This means that your strSelection is null. and when you call GetFirstImprintColors you are actually passing null to it, but it does not matter since you are not usign that string in the function anywho.

I would suggest following approach:

on load - populate both combo boxes, second combo visible = false;

go to the combo1_SelectedIndexChanged and specify that once the idex is changed, combo 1.visible = false and combo 2.visible is true;

go to the combo2_SelectedIndexChanged and do combo 2.visible = false; combo 1.visible = true.

Above events will ensure that once your user selects anything in the combo box 1, combo box 1 is now invisible and combo box 2 is visible. When user selects anything in combo box 2, combo 2 is invisible and combo 1 is visible

hope this helps

P.S. is this homework or something?

ok, in your GetProductsColor() you are populating the combo cmbProductColor - right? I would suggest doing cmbProductColor.Items.Clear(); before adding new stuff or you will append the values without getting rid of old values.

Later in the same function you are capturing selected item, well, the selected item is null because your user did not have a chance to actually seleect anything as of yet. This means that your strSelection is null. and when you call GetFirstImprintColors you are actually passing null to it, but it does not matter since you are not usign that string in the function anywho.

I would suggest following approach:

on load - populate both combo boxes, second combo visible = false;

go to the combo1_SelectedIndexChanged and specify that once the idex is changed, combo 1.visible = false and combo 2.visible is true;

go to the combo2_SelectedIndexChanged and do combo 2.visible = false; combo 1.visible = true.

Above events will ensure that once your user selects anything in the combo box 1, combo box 1 is now invisible and combo box 2 is visible. When user selects anything in combo box 2, combo 2 is invisible and combo 1 is visible

hope this helps

P.S. is this homework or something?

Thank You but I managed to solve it.

No it is not homework, I simply have never really worked with Forms before and I have never really done c# as well so this is very new to me. The more I work it the more I like it though and am amazed at how fast it is to both pick up and how much flexibility it has. I have always dreaded forms but this is great. A lot of the small things though still get to me as I am not sure of how to use thier properties.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.