0

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......

5
Contributors
5
Replies
7
Views
9 Years
Discussion Span
Last Post by hieuuk
0

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

0
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");
		...
	}
}
0

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

0

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)

0

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);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.