Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Windows form has three comboboxes. Combobox2 references a child table of combobox1, and Combobox3 references a child table of combobox2.

I want the three comboboxes not to show a selectedItem until the user selects a value.

I have this set when the Form is first shown:


private void Form1_Shown(Object sender, EventArgs e)
 {

     this.comboBox1.SelectedIndex = -1;
     MessageBox.Show("You are in the Form.Shown event.");

 }



private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     if ((isClosing == false)&&(comboBox1.SelectedValue!=null))
     {
         try
         {
             String cb1Value = comboBox1.SelectedValue.ToString();
               //fill child table code goes here

         }
         catch (Exception ex)
         { MessageBox.Show(ex.Message, ex.GetType().ToString() + " comboBox1_SelectedIndexChanged"); }
         try
         { this.comboBox2.SelectedIndex = -1; }//end try
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message, ex.GetType().ToString() + "cb2 ind comboBox1_SelectedIndexChanged ");
         }//end catch
     }//end if
 }//end comboBox1_SelectedIndexChanged


But combobox2 still has a selected value when I make a selection for combobox1!

In addition, I keep getting the following error message when these selections are made:
"Object reference not set to an instance of an object."

Does anyone know what is happening? Thanks in advance!
Posted
Updated 23-Mar-16 0:26am
v6
Comments
Marc A. Brown 16-Sep-10 11:48am    
Where (what line of code) are you hitting the error?
envprogrammer 16-Sep-10 11:57am    
I followed the method using "step into" with the debug mode. It executes "this.comboBox2.SelectedIndex = -1" successfully. Once it leaves comboBox1_SelectedIndexChanged, somehow combobox2 gets selected. The debugger doesn't show me what method is doing this even when I'm pressing F8. I think this is a case of Net "thinking" for me, but I don't know how.
Dalek Dave 17-Sep-10 3:30am    
Minor Edit for Grammar.
envprogrammer 17-Sep-10 15:36pm    
Thanks everyone for your help! I ended up using a "select null union all select *" query in the fill command as a work-around

Is there a typo in your if statement that opens comboBox!_SelectedIndexChanged? You've got comboBox1.SelectedValue1=null and I'm thinking it should be comboBox1.SelectedValue!=null.
 
Share this answer
 
Comments
envprogrammer 16-Sep-10 10:27am    
That typo is just here--that error is not in the code. I retyped things for here.
Dalek Dave 17-Sep-10 3:30am    
Well Spotted.
envprogrammer 17-Sep-10 15:36pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
First, I hope this is not another typo, but you have:
C#
if ((isClosing == false)&&(comboBox1.SelectedValue=null))


Without the double equal sign, comboBox1.SelectedValue will be set to null.

That would result in your call String cb1Value = comboBox1.SelectedValue.ToString(); raising an object reference not set to an instance of an object error.

As far as comboBox2 not being reset, I'm not sure. Setting the SelectedIndex to -1 should do that. Though, you really don't need it in a try/catch block.

Setting SelectedIndex = -1 will never throw an error unless comboBox2 has been set to null at some point.
 
Share this answer
 
Comments
envprogrammer 16-Sep-10 11:36am    
Was another typo--but I'll watch out for errors like that in the future.
Dalek Dave 17-Sep-10 3:30am    
Good Call.
envprogrammer 17-Sep-10 15:36pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
You can set SelectedIndex = -1, but always remind you that when you set it to -1 even though one item will seem to be selected, you cannot get its value from SelectedItem as it will return you null.

Please check SelectedItem to be null before calling any method.

:rose:
 
Share this answer
 
Comments
envprogrammer 17-Sep-10 15:36pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
I think the following is more or less what you are looking for.

C#
public Form1()
        {
            InitializeComponent();

            FillFirstCombobox();
            cmbSecond.SelectedIndexChanged += new EventHandler(cmbSecond_SelectedIndexChanged);
        }

        private void cmbFirst_SelectedIndexChanged(object sender, EventArgs e)
        {
           FillSecondCombobox(cmbFirst.SelectedValue.ToString());
           cmbSecond.SelectedIndex = -1;
        }

        private void cmbSecond_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSecond.SelectedIndex > -1)
            {
                FillThirdCombobox(cmbSecond.SelectedValue.ToString());
                cmbThird.SelectedIndex = -1;
            }
        }

        private void FillFirstCombobox()
        {
            string[] items = new string[5]{"Item1","Item2","Item3","Item4","Item5"};

            cmbFirst.DataSource = items;
        }

        private void FillSecondCombobox(string selectedItem)
        {
            string[] items = new string[5];
            for (int i = 0; i < items.Count(); i++)
            {
                items[i] = String.Format("{0}_{1}", selectedItem, i);
            }

            cmbSecond.DataSource = items;
        }

        private void FillThirdCombobox(string selectedItem)
        {
            string[] items = new string[5];
            for (int i = 0; i < items.Count(); i++)
            {
                items[i] = String.Format("{0}_{1}", selectedItem, i);
            }

            cmbThird.DataSource = items;
        }
 
Share this answer
 
Comments
Dalek Dave 17-Sep-10 3:31am    
Good Answer.
envprogrammer 17-Sep-10 15:36pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

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