简体   繁体   中英

C# -comboBox Selected IndexChange

I have a ComboBox that have a list of EmpolyeeNames. When a user selects a EmpolyeeName "e1", a ListBox below gets populated with data for the chosen employee. That data can be modified. The user and has to press the Save button after all changes are done.

But if the user forgets to press Save and select another employee from the ComboBox say "e2" , here i ask user mEssagebox "Do you want to save data for employee "e1" if yes then I save the data of particular employee "e1",

But here while saving the data combo box index gets changed and its text show recently selected employee "e2", but the data is of employee "e1".

HOw can i retain the old previous text of employye "e1" in comboBox until save gets completed.??

Quite simply, when the combobox item is selected put the employee into a class variable. Use this class variable instead of the item in the combobox.

After you have saved (or prompted) the user you can then set the variable to the newly selected item.

Your focus here should really be on how you are going to detect when the user has changed data in the listbox. You could put a flag somewhere that will be an indicator whether some data has been changed for that particular user. If for example it is the text that will change in the listbox item you could use the TextChanged event to set the flag.

Example:

bool employeeEdited = false;

private ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     if (employeeEdited)
     {
         // prompt user to save
     }
     // reset flag
     employeeEdited = false;
}

private void ListBox1_TextChanged(object sender, EventArgs e)
{
     employeeEdited = true;
}

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