如何取消winforms中的任何事件?

How to cancel any event in winforms?(如何取消winforms中的任何事件?)
本文介绍了如何取消winforms中的任何事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取消该函数范围内的事件.

I want to cancel an event from within that function scope.

例如.我按下按钮单击事件并在错误验证时,我想取消此事件.同样,我也想取消其他活动.

Eg. I pressed button click event and on false validation, I want to cancel this event. Likewise i want to cancel other events also.

我如何在 C# 中做到这一点

How can i do this in C#

推荐答案

视场景而定;在大多数情况下:而不是 cancel 事件,什么也不做,例如:

It depends on the scenario; in most cases: rather than cancel the event, just do nothing, for example:

private void SaveDataClicked(object sender, EventArgs args) {
    if(!ValidateData()) return;
    // [snip: code that does stuff]
}

或:

private void SaveDataClicked(object sender, EventArgs args) {
    if(ValidateData()) {
        // [snip: code that does stuff]
    }
}

有些事件会暴露 CancelEventArgs(或类似的),允许通过 args 取消一些 外部 行为 - 形式 -关闭是最明显的例子(设置 e.Cancel = true;).

There are some events that expose a CancelEventArgs (or similar), allowing to to cancel some external behaviour via the args - form-closing being the most obvious example (set e.Cancel = true;).

请注意,在这种情况下,我不会在按钮上显示自动对话结果;当(如果)处理程序成功完成时手动应用.

Note that in this scenario I would not have an automatic dialog-result on the button; apply that manually when (if) the handler completes successfully.

这篇关于如何取消winforms中的任何事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)