简体   繁体   中英

How to go back to previously selected entry in a combobox?

In my application there is a requirement like this:

In a form there is a combo box and 4 textboxes. The combo box always contain range of values 1...10. Based on the value selected in the combo box, I need to read 4 data items corresponding to the selected value from a database and display these values in the 4 textboxes. Thus for each value in the combo box, there are 4 values in the database.

Also, user can change these values by editing the values in 4 text boxes.

As an example assume that currently selected value in the combo box is 1. So the data corresponding to record 1 is read from the database and shown in the 4 text boxes. If the user don't edit any value in the textbox and selects entry 2 (or any other value than 1) then the data corresponding to the newly selected value (ie 2) is fetched from the database and displayed in the 4 textboxes. But if the user edited any value by typing into any of the four available textboxes then those new values corresponding to record 1 should be saved to the database before switching to the newly selected entry, ie 2.

To do this I wrote some code in the "SelectedIndexChanged event handler" of the combo box. There I check whether the user has changed any value. If yes, I will prompt the user to save the data. After saving the data, the combo box will show the newly selected value by user ie 2 (and the 4 text boxes will show data corresponding to record 2). All the above mentioned functionalities work fine. But there is an additional requirement "If the save operation to the database fails, then the initial value in the combo box should be shown along with the user edited data (in the textboxes) which means if the database write fails I have to show the currently selected value 1 without switching to the newly selected value 2".

In the SelectedIndexChanged handler I tried writing the below code.

If (DatabaseWriteFails)
{
    ComboBox->SelectedIndex = previous_value; (previous_value is 1 in our example)
}

But the above code did not work (I think since we are trying to change the selected value from SelectedIndexChanged handler itself). When we enter the SelectedIndexChanged handler I can see that ComboBox->SelectedValue is already changed to the newly selected value ie 2.

The question is "how we can go back to the previous entry once we enter the SelectedIndexChanged handler"? Any help is appreciated. Thank you.

First of all, if you change the ComboBox value from the event, it will trigger that event again, so instead of using SelectedIndexChanged use SelectionChangeCommitted which occurs only when the user changes the selected item from UI, not programmatically.

Second, you should change the dropdown by using either SelectedItem or SelectedIndex, from my experience SelectedValue binding doesn't update the ComboBox.

My advice for you as mentioned in the comments is to use bindings or even ReactiveUI .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM