如何取消的WinForms无论如何?无论如何、WinForms

2023-09-03 05:09:54 作者:孤癌

我想从这个函数范围内取消的事件。

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

例如。我pressed按钮单击事件和错误的验证,我想取消本次活动。同样,我想取消其他事件也。

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#

推荐答案

这取决于具体的方案;在大多数情况下:不是的取消的情况下,只是什么也不做,例如:

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.

 
精彩推荐
图片推荐