C# -comboBox Selected IndexChange

落花浮王杯 提交于 2019-12-25 03:38:32

问题


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


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/1558686/c-sharp-combobox-selected-indexchange

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!