在DialogResult.OK按钮“退出小组” [英] 'Exit Sub' on DialogResult.OK button

查看:244
本文介绍了在DialogResult.OK按钮“退出小组”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB.NET形式(CopyScenarioForm)与OK键(DialogResult属性= OK),并指定为'接受按钮的形式。

I have a VB.NET form (CopyScenarioForm) with a OK button (DialogResult property = OK) and also assigned it as 'Accept Button" for the Form.

我从我的主要形式(MainForm中)使用证明这种形式

I show this form from my main Form (mainForm) using

If DialogResult.OK = CopyScenarioForm.ShowDialog() Then
      DoSomething()
End if

现在,当用户点击CopyScenarioForm.OK按钮,我想验证自己的作品,如果无效,我想从OK按钮的点击处理程序退出小组,但是当我这样做,形式仍然关闭了和DoSomething的()被执行。有没有办法制止这种并保持形态活着,唯一的出口,如果输入是有效的。我发现,如果我确定按钮的DialogResult属性变更,而不是'确定'为'none',那么它不会导致它关闭。但我怎么知道用户如何退出的形式来执行的DoSomething()。

Now when user clicks on the CopyScenarioForm.OK button, I want to validate his entries and if invalid I want to 'Exit Sub' from the OK button's click handler but when I do that the form still closes out and DoSomething() gets executed. Is there a way to stop this and keep the form alive and only exit if the inputs are valid. I noticed, if I change the OK button's DialogResult property to 'NONE' instead of 'OK' then it doesn't cause it to close. but then how Do I know how the user exited the form to execute DoSomething().

推荐答案

发生了什么事是,当你有按钮的的DialogResult 属性设置为OK的设计师,此值每次单击确定按钮,无论在什么时候设置。所以,即使你从早期使用退出小组事件处理程序退出,调用窗体看到的DialogResult 的OK。

What's happening is that when you have the button's DialogResult property set to "OK" in the designer, this value is set every time the OK button is clicked, no matter what. So even when you exit from the event handler early using Exit Sub, the calling form sees a DialogResult of "OK."

当你发现,你必须先为无在设计师设置按钮的的DialogResult 属性,然后处理设置 DialogResult的属性为正确的值手动在您的确定按钮的单击事件处理程序。例如:

As you discovered, you'll first need to set the button's DialogResult property to "None" in the designer, and then handle setting the DialogResult property to the correct value manually in your "OK" button's click event handler. For example:

Private Sub OKButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If EntriesAreValid Then
        'Return OK to the calling form
        Me.DialogResult = DialogResult.OK
    Else
        'Show an error message, but keep the form open
        MessageBox.Show("One or more of your entries were invalid.")
    End If
End Sub

另外,你可以离开的DialogResult 属性设置为OK的设计师,每当验证失败设置它只是覆盖它为无。这可能会产生更清洁的code-即:

Alternatively, you could leave the DialogResult property set to "OK" in the designer, and just override it whenever validation fails by setting it to "None". This probably produces cleaner code—to wit:

Private Sub OKButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If Not EntriesAreValid Then
        'Show an error message
        MessageBox.Show("One or more of your entries were invalid.")

        'Clear the DialogResult property and keep the form open
        Me.DialogResult = DialogResult.None
    End If
End Sub

这篇关于在DialogResult.OK按钮“退出小组”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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