如果单击取消按钮,如何忽略离开事件? [英] How to ignore a leave event if a cancel button is clicked?

查看:129
本文介绍了如果单击取消按钮,如何忽略离开事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简单的离开事件,该事件来自Visual Studio形式的文本框控件:

Consider this simple leave event from a text box control in a visual studio form:

private void dscr_Leave(object sender, EventArgs e)
{
    if (dscr.Text == null)
        MessageBox.Show("You must supply a description");
}


假设屏幕上有一个取消"按钮,焦点位于"dscr"文本框中,并且为空白.您单击取消按钮.作为一个初学者,我正在努力如何在单击取消按钮时忽略此请假事件.
我已经搜索了很长时间,但是找不到如何完成此操作.离开事件将在点击之前触发,但是必须有某种方式可以知道点击事件即将到来,并避免不必要的验证.

谁能指出我如何处理这种情况的任何示例,或指导我如何完成此操作?


Assume there is a "cancel" button on the screen, and focus is in the "dscr" text box, and it''s blank. You click the cancel button. As a beginner, I''m struggling on how to ignore this leave event when a cancel button is clicked.
I''ve searched quite a while, and cannot find how to accomplish this. The leave event will fire before the click, but there''s got to be some way to know the click event is coming, and avoid unnecessary validation.

Can anyone point me to any examples of how to handle this situation, or direct me as to how to accomplish this?

推荐答案

我个人会移动您的


personally I would move your


if (dscr.Text == null)
            MessageBox.Show("You must supply a description");



进入下一步按钮保存或更新按钮.



into the next step button e.g. save or update button.


Simon_Whale是绝对正确的.将这样的检查直接放在编辑按钮上从来不是一个好主意,因为您甚至不能正常关闭表单而不会收到错误.用户将被不必要地困住.

在单击下一步"之前,给予用户一定的自由度也更为用户友好.最好用CheckFormInput()之类的单个函数而不是每个输入控件的每个Leave事件来完成检查.

祝你好运!
Simon_Whale is absolutely right. It is never a good idea to put such a check directly on an edit button because you couldn''t even close the form normally without getting the error. The user would be trapped unnecessarily.

It is also more user friendly to give the user some freedom until Next is clicked. It is also nicer to have your checks done in a single function like CheckFormInput() instead of each Leave event of each input control individually.

Good luck!


这篇关于如果单击取消按钮,如何忽略离开事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆