我们使用这种编码来处理大红色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
所以很烦人当人们downvote良好的问题 – alwaysVBNET 2014-06-06 15:44:13
你应该被迫给出你投票的原因。在我自己的问题中,总是让我疯狂。好的,你可以投票,但我想要一个理由。 – 2015-07-19 23:42:00