【问题标题】:Disabling validation on all forms when user closes application用户关闭应用程序时禁用所有表单的验证
【发布时间】:2012-02-26 16:44:21
【问题描述】:

当用户单击“X”图标关闭应用程序或用户单击应用程序中关闭应用程序的按钮时,是否有一种简单的方法可以禁用所有表单验证?

我找到了这个,但它在 C# 中。你能把它转换成 VB.Net 编码吗?

http://geekswithblogs.net/dapostolov/archive/2009/06/14/the-validating-event-can-prevent-a-form-closing-properly.aspx

【问题讨论】:

    标签: vb.net winforms validation


    【解决方案1】:

    找到了!

    我把这段代码放在按钮点击处理程序中:

    ' Disable validation on the form.
    '--------------------------------
    Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
    

    当我再次调用表单时,我将其用于名为 objFormParents 的表单对象:

    ' Reset validation on this form because the user may have closed it before.
    '--------------------------------------------------------------------------
    objFormParents.CausesValidation = True
    

    我在互联网上找到了这个来处理点击“X”图标:

    ' This will allow the user to close the form without the worry of controls doing validation from "X".
    '----------------------------------------------------------------------------------------------------
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    
        Select Case ((m.WParam.ToInt64() And &HFFFF) And &HFFF0)
    
            Case &HF060 ' The user chose to close the form.
                Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
        End Select
    
        MyBase.WndProc(m)
    End Sub
    

    【讨论】:

      【解决方案2】:

      你可以做的更简单。

      Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
          If m.WParam.ToInt32 = &HF060 Then Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable
          MyBase.WndProc(m)
      End Sub
      

      【讨论】:

      • 感谢您的回答。
      【解决方案3】:

      我找到了实现此解决方案的另一种方法

      首先你声明一个公共属性

      Private bFormClosing As Boolean = False
      

      然后你在你的验证代码中包含这个布尔值和类似

      . . . And bFormClosing = False then
      

      你在这里使用

          ErrorProvider.SetError("My Error Message")
          e.Cancel = True
      Else 
          e.Cancel = False
      

      您为所有验证控制事件执行此技巧,然后在验证事件之前触发的Exit_Button 的输入事件中,设置bClosingForm = true

      然后它触发一个级联事件并转到验证事件以执行AND bFormClosing,如果您有else,它将跳转到e.Cancel = false,并且将禁用验证并允许您关闭表单所有控件验证事件。

      记得将退出按钮的CausesValidation=false设置为false。

      您也可以将它放在鼠标按下甚至 ClosingForm 事件中,任何在验证事件之前触发的事件。万岁,终于为我工作了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多