Problem with Form and Combox selections

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Problem with Form and Combox selections

 
0
  #1
Oct 17th, 2007
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

  1. private void optProductColor_CheckedChanged(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. if (optProductColor.Checked == true)
  6. {
  7. this.lblProductColor.Visible = true;
  8. this.cmbProductColor.Visible = true;
  9. this.lblCustomerType.Visible = false;
  10. this.cmbCustomerType.Visible = false;
  11.  
  12. GetProductColors();
  13. }
  14.  
  15. }
  16. catch (Exception ex)
  17. {
  18. Console.WriteLine(ex.Message);
  19. }
  20.  
  21. }
Here is the next function

  1. private void GetProductColors()
  2. {
  3. try
  4. { //DB calls to Stored Procedure
  5. //Read the DB into our ComboBox
  6. while (oleDbDataReader1.Read() == true)
  7. {
  8. this.cmbProductColor.Items.Add(oleDbDataReader1.GetString(0));
  9. }
  10. }
  11. catch (Exception ex)
  12. {
  13. Console.WriteLine(ex.Message);
  14. }
  15. finally
  16. {
  17. oleDbConnection1.Close();
  18. }
  19.  
  20. //Get Selection from Combo Box
  21. string strSelection = cmbProductColor.SelectedItem.ToString();
  22. //Make next combobox visible
  23. this.cmbFirstColor.Visible = true;
  24.  
  25. //Send String to First Imprint Color
  26. GetFirstImprintColors(strSelection);
  27. }
This calls the next function which is not displayed until I leave the radio button and then come back

  1. private void GetFirstImprintColors(string strSelection)
  2. {
  3. try
  4. {
  5. //See if DB is open
  6. //DB calls
  7. //Read the DB into our ComboBox
  8. while (oleDbDataReader1.Read() == true)
  9. {
  10. this.cmbFirstColor.Items.Add(oleDbDataReader1.GetString(0));
  11. }
  12. }
  13. catch (Exception ex)
  14. {
  15. Console.WriteLine(ex.Message);
  16. }
  17. finally
  18. {
  19. oleDbConnection1.Close();
  20. }
  21.  
  22. }

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

Thanks
Last edited by blazted; Oct 17th, 2007 at 1:04 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Re: Problem with Form and Combox selections

 
0
  #2
Oct 18th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: tostrinj is an unknown quantity at this point 
Solved Threads: 1
tostrinj's Avatar
tostrinj tostrinj is offline Offline
Newbie Poster

Re: Problem with Form and Combox selections

 
0
  #3
Oct 26th, 2007
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?
===========================
can you repeat the part of the stuff where you said all about the things?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Re: Problem with Form and Combox selections

 
0
  #4
Oct 27th, 2007
Originally Posted by tostrinj View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC