我在表单顶部有一个组合框,可将可编辑数据加载到下面的字段中。如果用户进行了更改但未保存,并尝试从组合框中选择不同的选项,我想警告他们并给他们取消或保存的机会。
我需要一个带有可取消事件参数的“BeforeValueChange”事件。
有什么关于如何完成的建议吗?
最佳答案
如果第一次输入时保存 ComboBox 的 SelectedIndex,然后在需要取消更改时恢复它的值。
cbx_Example.Enter += cbx_Example_Enter;
cbx_Example.SelectionChangeCommitted += cbx_Example_SelectionChangeCommitted;
...
private int prevExampleIndex = 0;
private void cbx_Example_Enter(object sender, EventArgs e)
{
prevExampleIndex = cbx_Example.SelectedIndex;
}
private void cbx_Example_SelectionChangeCommitted(object sender, EventArgs e)
{
// some custom flag to determine Edit mode
if (mode == FormModes.EDIT)
{
cbx_Example.SelectedIndex = prevExampleIndex;
}
}
关于c# - 如何防止/取消 c# 中组合框的值更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/314503/