c# combo boxes help

Status
Not open for further replies.

Chad711

Daemon Poster
Messages
1,444
Location
Irving, TX
The idea here is to have two combo boxes. Once you make a selection in the first combo box you will then have options in the 2nd combo box depending on what you selected in the first box.

Well if you change cmbx1 input then cmbx2 should clear., and it does. However if you try to change cmbx1 back to the first input it gives me a debug error.

Here is what I have so far.

namespace AutoFactory
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private string[] _make = { "Chevy", "Ford"};
private string[] _chevyModel = { "Corvette", "Tahoe", "Avalanche"};
private string[] _fordModel = { "Mustang", "Explorer", "F-150"};

public Window1()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
comboBox1.ItemsSource = _make;
comboBox2.SelectedIndex = 0;
comboBox2.ItemsSource = _chevyModel;
}

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBox1.SelectedIndex == 0)

comboBox2.Items.Clear();
comboBox2.SelectedIndex = 0; // Ford
comboBox2.ItemsSource = _fordModel; // then Ford models

}

private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

}

}
}

EDIT: I have decided to move to radio buttons instead because I need to get this assignment done ASAP. However I would like to learn anything I can so if someone gets a chance to answer this that would be great. Going to make a new thread with my question for new app form.
 
comboBox2.SelectedIndex = 0; // Ford
comboBox2.ItemsSource = _fordModel; // then Ford models

You need to swap the two lines around - because you are emptying the box then trying to select the first index (which doesnt exist until you put the information into the box with the second line). Like this:

comboBox2.ItemsSource = _fordModel; // then Ford models
comboBox2.SelectedIndex = 0; // Ford
 
I see that you are using WPF. Just out of curiosity, is this a requirement of your assignment?
 
Status
Not open for further replies.
Back
Top Bottom