Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
hello, iam adnan from indonesian
to the point

i want to make Combo Box fill Combo Box,
case : i have 2 Combo Box, Combo Box 1 and Combo Box 2, when i click one of item in Combo Box 1 (ex : Math Class) , Combo Box 2 will automatically fill the name of student in Math class,

My assumption : i make some database form microsoft office access database, or i make it from source?
can you help me what must i do?

i really appreciate if you help me, thx

adnan
Posted

Selamat Sore,

What have you done, and tried so far ?

Have you created two ComboBoxes, filled them with Items, and made sure you understand how to change what's in the second ComboBox by using an EventHandler you've created for the first ComboBox for one of these events: SelectedIndexChanged, SelectedValueChanged, or SelectionChangeCommitted ?

Perhaps, consider parsing simple Text files to hold the contents of the second ComboBox item-sets. One separate file for each of the different groups ?

~

Visual Studio and the Designer.cs file it produces (C#, WinForms) can be a great asset to you here: after you drag-drop a first ComboBox onto a Form, context-click on it, and select "Edit Items:" then type in a bunch of entries, separated by carriage returns. Close that item-editor.

Then,open the Designer.cs file: click on the little rectangle with the "+" sign to expand the region titled "Windows Form Designer generated code."

You'll see that the way that Items are added to a ComboBox looks something like this:
C#
this.cmbPrimary.Items.AddRange
(
    new object[] 
    {
        "Category1",
        "Category2",
        "Category3"
    }
);
That shows you clearly the form your different item-sets for the second ComboBox are going to have to be in, and you are going to have to get your data, whatever its source, into that form. There are many ways to do that.

~

Personally, I would probably store all the Item-set data in either one Text file, or, perhaps one XML format file, and then parse it: so, I'd build an object like this:
C#
List<Object[]> SecondaryComboBoxItems = new List<object[]>()
{
    new object[] { "Item 1", "Item 2", "Item 3", "Item 4" },
    new object[] { "Item 1a", "Item 2a", "Item 3a", "Item 4a"},
    new object[] { "Item 1b", "Item 2b", "Item 3b", "Item 4b"}
};
This gives me a List, each Item of which is an Array of Objects where each object in a given array is a string.

So, I could just use an integer index (in this case #0~2) to get the right Items into the secondary ComboBox.

~

The Database issue: if you use a DataBase, one thing you probably don't want to do is: every time the selection in the first ComboBox changes you must pull-down the data again: in your scenario here, you are probably not talking about such "massive data" that you would need to, possibly, do this.

So, if you use a DataBase: how can you get all the different groups of whatever that will go into the second ComboBox's Item-sets, and store ("cache") them: so switching between Item-sets in the second ComboBox, as the selected Item in the first ComboBox changes: is fast ?

This is not difficult, and is a good problem for you to solve.

~

If you break down this challenge into steps, as I've tried to illustrate here (this is called the "divide and conquer" strategy" by some teachers of programming), and solve each step:

You will learn a set of skills that you can then apply to many different future problems, and my goal in this response is to get you to work on that, not just telling you how to write the code.

Baik baik saja ?

good luck, Bill
 
Share this answer
 
Pull info from database and use the selectedindexchanged event. When 1 combobox is filled, the 2nd one will be filled with corresponding information relating to the prior selection.
 
Share this answer
 
Comments
adnanaries 27-Sep-12 15:07pm    
so the first thing i do is make database and use the SelectedIndexChanged event ,right?
joshrduncan2012 27-Sep-12 15:10pm    
1) Fill database with proper data
2) Set up your comboboxes
3) Use the selectedindexchanged event to fire off the events. If a selection is made in 1 box, the second box will be enabled or populate with data, etc.

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