简体   繁体   English

C#-comboBox选定的IndexChange

[英]C# -comboBox Selected IndexChange

I have a ComboBox that have a list of EmpolyeeNames. 我有一个具有EmpolyeeNames列表的ComboBox。 When a user selects a EmpolyeeName "e1", a ListBox below gets populated with data for the chosen employee. 当用户选择EmpolyeeName“ e1”时,下面的列表框将填充所选雇员的数据。 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", 但是,如果用户忘记按“保存”并从组合框中选择另一个雇员说“ e2”,在这里我问用户mEssagebox“是否要为雇员“ e1”保存数据,然后我保存特定雇员“ 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". 但是,在保存数据的同时,组合框索引已更改,其文本显示最近选择的员工“ e2”,但数据来自员工“ e1”。

HOw can i retain the old previous text of employye "e1" in comboBox until save gets completed.?? 在保存完成之前,我如何才能在comboBox中保留employeeye“ e1”的旧文本。

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. 例如,如果是列表框项目中将要更改的文本,则可以使用TextChanged事件来设置标志。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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