2012-07-15 138 views
0

我们使用这种编码来处理大红色X的单击,以此作为绕过窗体上所有文本框验证的手段。防止VB.Net窗体关闭

该代码将测试是否对窗体上的数据绑定控件进行了任何更改。代码句柄取消在关闭表单之前所做的更改。

还想取消点击大X并且不允许表单关闭。

你可以显示任何需要的编码,不允许窗体实际关闭吗?我们希望在下面的编码演示中的Else声明之后添加这个新编码。

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.StudentsBindingSource.EndEdit() 
      Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable 

      If Me.StudentsDataSet.HasChanges Then 

       ' Alert the user. 
       '---------------- 
       If MessageBox.Show("You are about to loose any *** Student *** changes you have made! " & vbCrLf & vbCrLf & _ 
            "ARE YOU SURE YOU WANT TO DO THIS?" & vbCrLf & vbCrLf, _ 
            "*** W A R N I N G ***", _ 
            MessageBoxButtons.YesNo, _ 
            MessageBoxIcon.Warning, _ 
            MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then 

        RibbonButtonCancelChanges_Click(Nothing, Nothing) 
       Else 
        ' Reset validation. 
        '------------------ 
        Me.CausesValidation = True 
       End If 
      End If 
    End Select 

    MyBase.WndProc(m) 
End Sub 

我们试过这个,但是文本框控件的Validating事件执行的并不是我们想要的。

Private Sub FormStudents_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 

    Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable 
    Me.StudentsBindingSource.EndEdit() 

    If Me.StudentsDataSet.HasChanges Then 

     ' Alert the user. 
     '---------------- 
     If MessageBox.Show("You are about to loose any *** Student *** changes you have made! " & vbCrLf & vbCrLf & _ 
          "ARE YOU SURE YOU WANT TO DO THIS?" & vbCrLf & vbCrLf, _ 
          "*** W A R N I N G ***", _ 
          MessageBoxButtons.YesNo, _ 
          MessageBoxIcon.Warning, _ 
          MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then 

      RibbonButtonCancelChanges_Click(Nothing, Nothing) 
     Else 
      ' Reset validation. 
      '------------------ 
      Me.CausesValidation = True 

      e.Cancel = True 
     End If 
    End If 
End Sub 
+2

所以很烦人当人们downvote良好的问题 – alwaysVBNET 2014-06-06 15:44:13

+1

你应该被迫给出你投票的原因。在我自己的问题中,总是让我疯狂。好的,你可以投票,但我想要一个理由。 – 2015-07-19 23:42:00

回答

9

根本不应该使用WndProc

取而代之的是,处理FormClosing事件并将e.Cancel设置为true。

+1

感谢您的快速回复。这也会绕过文本框验证? – 2012-07-15 20:21:05

+1

刚刚尝试过,e.Cancel工程很好,但文本框验证执行。有没有办法取消在这种情况下执行文本框验证? – 2012-07-15 20:28:42

+1

我得到它与FormClosing和WndProc的组合工作。谢谢大家的帮助。 :-) – 2012-07-15 23:10:56

2

正如SLaks所说,您应该使用FormClosing()事件过程,而不是在WndPorc()中引入复杂性。重写WndProc()用于像C++这样的语言,在这种语言中,你没有豪华的事件过程来处理这些事件。但VB.NET的简单性为您提供了一个名为FormClosing()的事件过程。只需打开代码并在对象下拉列表中选择您的表单名称(左侧),然后从事件下拉列表中选择FormClosing(右侧)。这应该给你一个模板来写你的事件代码,像这样:

Private Sub FormClosing(Source as Object, e as EventArgs) Handles MyForm.Closing 
    e.Cancel = True 
End Sub 

只需添加“e.Cancel =真”,如上图所示,并且形式将永远不会关闭!

+0

感谢您的回复。 e.Cancel = True很好,但现在TextBox控件的Validating事件执行,这就是为什么我们发现使用WndProc()覆盖Validating事件。 – 2012-07-15 20:39:33

+0

如果你真的想要,你仍然可以避免WndProc()。如果要避免要触发文本框的验证事件,可以将文本框控件的CausesValidation属性设置为False。 – 2012-07-18 09:43:16

0

谢谢你让我知道的FormClosing和e.Cancel

我能够使用该处理所需要的一切的FormClosing和的WndProc的组合。

我加入这个右边的表格类名之后:

Dim blneCancel As Boolean = False 

我的WndProc现在看起来是这样。注意blneCancel的设置。

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.StudentsBindingSource.EndEdit() 
      Me.AutoValidate = System.Windows.Forms.AutoValidate.Disable 

      If Me.StudentsDataSet.HasChanges Then 

       ' Alert the user. 
       '---------------- 
       If MessageBox.Show("You are about to loose any *** Student *** changes you have made! " & vbCrLf & vbCrLf & _ 
            "ARE YOU SURE YOU WANT TO DO THIS?" & vbCrLf & vbCrLf, _ 
            "*** W A R N I N G ***", _ 
            MessageBoxButtons.YesNo, _ 
            MessageBoxIcon.Warning, _ 
            MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then 

        RibbonButtonCancelChanges_Click(Nothing, Nothing) 
       Else 
        ' Reset validation. 
        '------------------ 
        Me.CausesValidation = True 
        blneCancel = True 
       End If 
      End If 
    End Select 

    MyBase.WndProc(m) 
End Sub 

该程序的FormClosing看起来是这样的:

Private Sub FormStudents_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 

    If blneCancel = True Then 
     e.Cancel = True 
    End If 
End Sub 

现在,用户可以输入电话号码文本框什么,如果用户点击了大X关闭形式,它不会验证。表单将显示警告用户某些内容已更改的消息,并让他们选择退回并尝试保存更改或退出而不保存任何内容。