取消列表框SelectedIndexChange事件 [英] Cancelling ListBox SelectedIndexChange Event
问题描述
是否有可能取消SelectedIndexChange事件在WinForms应用程序列表框?这似乎是个顺理成章的事情有,我必须俯瞰一些简单的功能。基本上,我一直弹出一个消息框,询问用户是否真的要移动到另一个项目,因为这将改变用户界面,我不想失去他们的更改。我希望能够取消情况下,用户没有保存他们正在处理的事件。是否有这样做的更好的办法?
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.
的问题是,这将使得该事件火再次。所以,我所采用的是只是说禁止一个bool。而在该事件处理程序的顶部我有:
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.
这篇关于取消列表框SelectedIndexChange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!