I am currently developing a WPF app in which there are multiple forms to manage a database. One of these forms is a "Add form". This form contains a combobox with some values. Though if the user's wanted value is not in the combobox list, it can be added by selecting the last option which will display a new form for entering the wanted value. If the form is submitted, the last option in the combobox will be changed to this value. The code below describes how this works:
private void BrandBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (BrandBox.SelectedItem != null &&
BrandBox.SelectedIndex < BrandBox.Items.Count - 1)
{
InitSubCategoryBox(subCatArray[BrandBox.SelectedIndex]);
}
else if (BrandBox.SelectedItem != null &&
BrandBox.SelectedIndex >= BrandBox.Items.Count - 1)
{
OtherInputWindow newInputForm = new OtherInputWindow("brand", BrandBox.SelectedItem.ToString());
if (newInputForm.ShowDialog() == true)
{
BrandBox.Items[BrandBox.Items.Count - 1] = newInputForm.returnValue;
BrandBox.SelectedIndex = BrandBox.Items.Count - 1;
}
}
}
The last code line will set the submitted value as selected item. I know this does not work. This last line of code will create a SelectionChanged event and that is exactly what this method is. The result is that it will keep looping and there is no way out. I'm searching for the right way to do this for a long time but I wasn't able yet to find the answer. Hopefully I will find the answer here. Is there someone who can help me solving this problem?