是否可以取消 Winforms 应用程序上列表框的 SelectedIndexChange 事件?这似乎是一件合乎逻辑的事情,以至于我必须忽略一些简单的功能.基本上,我一直在弹出一个消息框,询问用户是否真的想要移动到另一个项目,因为这会改变 UI,我不希望他们的更改丢失.如果用户没有保存他们正在处理的内容,我希望能够取消该事件.有更好的方法吗?
Is it possible to cancel the SelectedIndexChange event for a listbox on a winforms application? This seems like such a logical thing to have that I must be overlooking some easy feature. Basically, I have been popping up a message box asking if the user really wants to move to another item, as this will change the UI and I don't want their changes to be lost. I'd like to be able to cancel the event in case the user has not saved what they are working on. Is there a better way of doing this?
你不能取消它.
几天前我所做的是使用最新选择的索引创建一个变量.然后当事件触发时,您询问用户是否要保存,这是在事件处理程序中完成的.如果用户选择取消",您将再次更改 id.
What I did just a couple of days ago was to have a variable with the latest selected index. Then when the event fires, you ask the user if he wants to save, this is done in the eventhandler. If the user selected "Cancel" you change the id again.
问题是这会使事件再次触发.所以我使用的是一个布尔值,只是说抑制".在事件处理程序的顶部,我有:
The problem is that this will make the event fire once again. So what i've used is a bool just saying "Inhibit". And at the top of the eventhandler I have:
if(Inhibit)
return;
然后在下面你问问题的地方你做这样的事情:
Then below this where you ask the question you do something like this:
DialogResult result = MessageBox.Show("yadadadad", yadada cancel etc);
if(result == DialogResult.Cancel){
Inhibit = true; //Make sure that the event does not fire again
list.SelectedIndex = LastSelectedIndex; //your variable
Inhibit = false; //Enable the event again
}
LastSelectedIndex = list.SelectedIndex; // Save latest index.
这篇关于取消 ListBox SelectedIndexChange 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!