如何防止/取消 C# 中组合框的值更改? [英] How to prevent/cancel a combobox's value change in c#?
本文介绍了如何防止/取消 C# 中组合框的值更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表单顶部有一个组合框,可将可编辑数据加载到下方的字段中.如果用户进行了更改但未保存,并尝试从组合框中选择不同的选项,我想警告他们并给他们取消或保存的机会.
I have a combobox at the top of a form that loads editable data into fields below. If the user has made changes, but not saved, and tries to select a different option from the combobox, I want to warn them and give them a chance to cancel or save.
我需要一个带有可取消事件参数的BeforeValueChange"事件.
I am in need of a "BeforeValueChange" event with a cancelable event argument.
关于如何完成的任何建议?
Any advice on how to accomplish?
推荐答案
如果第一次输入,则保存 ComboBox 的 SelectedIndex when to box,然后在需要取消更改时恢复它的值.
Save the ComboBox's SelectedIndex when to box if first entered, and then restore it's value when you need to cancel the change.
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# 中组合框的值更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文