hi all,

I am new to C#.I am doing a project on C#. In my project I am using two combobox. In first i am giving the options. Depending upon the first combobox options (i.e which item i am selecting) , the second combox should show its options relating to first combo box item.

ex: in first combo box if i give options like Days, Months
If i select option Days in first combobox ,the second should show Mon, tue...
If i select option Months in first, the second should show Jan , Feb....

how can i solve this problem......

Recommended Answers

All 5 Replies

Hi, You have to add values dynamically to the second combo box that is in the SelectedIndexChanged event of ComboBox1.

private void comboBox3_SelectedIndexChanged(object sender, System.EventArgs e)
{
	if (comboBox2.SelectedItem = "Days")
	{
		comboBox3.Items.Add("Mon");
		comboBox3.Items.Add("Tue");
		...
	}
	else if (comboBox2.SelectedItem = "Months")
	{
		comboBox3.Items.Add("Jan");
		comboBox3.Items.Add("Feb");
		...
	}
}

Or you can create a few arrays and instead of using if you can also use select case (or is this vb?)

hi all,
I used the above code for the two combox. Its working fine. But my problem is first when i select 1st item in 1st combobox, the second combobox will display its respective item related to 1st combobox.
If i select another item in 1st combobox, the second item will display its respective item. But the previous item of second combobox will be there itself until i select the second combobox. So the second combobox should refresh as soon as i select another item in the 1st combobox(i.e it should display blank or item of the respective selected item of 1st combo box)

Well, the code above is forget to write this on top.

comboBox3.Items.Clear();

But I would prefer this ways then the code above:

string Months[] = {"Jan"," Feb"};
String Days[] = {"Monday","Tuesday"};

private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
	comboBox3.Items.Clear();
	if(comboBox2.SelectedItem = "Month")
		comboBox3.Items.AddRange(Months);
	else
		comboBox3.Items.AddRange(Days);
}
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.