Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have combobox1 and comboox2 and in combobox1 my elements are A,B,C and combobox2 a,b,c,d,e,f...And I added this elements using property screeen(I mean not with code).

VB
combobox1                     combobox2
    -----------------            ----------------
           A                            a
           B                            b
           C                            c
                                        d
                                        e
                                        f
A related a,b,c and B related b,c and C related d,e,f.



When I choose "A" I want to see just a,b,c ; when select "B" just b,c etc.

can I give index number of combobox1's items ? When I choose "A" I want to see just "a","b","c" elements(I mean 0,1,2 indeks numbers) when choose "B" just b,c (I mean 1,2 indeks numbers). I want to do like that cause if there is no indeks numbers I have to make all relationship with writing codes.I wrote a little code below but it is long way.


SQL
Index logic is better, sure if there is index logic in combobox :)

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  {
     comboBox2.Items.Clear();
     switch (comboBox1.SelectedItem.ToString())
      {
        case "A":
           comboBox2.Items.AddRange(new string[] { "a", "b", "c" });
           break;

        case "B":
           comboBox2.Items.AddRange(new string[] { "b", "c" });
           break;
      }
       comboBox2.SelectedIndex = -1;
   }
Posted
Updated 24-Jul-12 21:04pm
v4
Comments
Sergey Alexandrovich Kryukov 25-Jul-12 2:45am    
To start with, tell us the exact type of ComboBox: there are more then one type with such simple name. This matter is extremely simple; I cannot get where is your difficulty...
--SA
y.baris 25-Jul-12 2:54am    
I added from toolbox and just normal ComboBox's. Problem is : when I select "A" I want to see just 0,1,2 index numbers of elements. And "B" the same etc.
Sergey Alexandrovich Kryukov 27-Jul-12 17:08pm    
There is no such thing as "normal" ComboBox. There are several types named "ComboBox". Please tell us which exactly. Are you too lazy to type its full name?
--SA
y.baris 30-Jul-12 2:02am    
I didn't understand you.When I open C# project, in toolbox there is ComboBox and I just added ComboBox1 from ToolBox into my Form.
I don't know exactly which type.I hope I am clear.
Sergey Alexandrovich Kryukov 30-Jul-12 2:36am    
If you don't know the type of your control when you write code, you are not programming, just fooling around.

Read your auto-generated code file. It could be something like System.Windows.Forms.ComboBox, System.Windows.Controls.ComboBox...

--SA

1 solution

The connection between the items in the first combobox and the second combobox is modelled also somewhere else (at least ought to be), e.g. in a table in a database or in a linking class.
That means, in the SelectedIndexChanged event of the first combobox (note: check that it is not -1!), you get the elements from the database table filtering for the value of combobox1, or you filter the data in the linking class with e.g. Linq, and then fill the second combobox.
You are already on the right way, just use that link between the data.
 
Share this answer
 
Comments
y.baris 25-Jul-12 3:17am    
Cause I want to do this : when I choose "A" I want to see in comboBox2 just 1,2 index numbers of elements.same "B" I want to see in comboBox2 just 4,5,6 index numbers of elements.
Bernhard Hiller 25-Jul-12 3:23am    
Analyze that "I want to see" thing: what does it mean? That there is some connection/link between the elements. And that connection must be modelled in proper design and code.
Also think: how will you update it, when more items are added?
There is absolutely no need to repeat that "when I chose... want to see..." - we understand that!
y.baris 25-Jul-12 3:42am    
Ok you understood ).I think you offer like my code above.And maybe my question meanless.Ok then forget about it.
Also; Can I do using with xml.Country's(comboBox1) and city's(comboBox2) relationship I want to do.And sorry for a lot of question

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