我们正在使用此编码来处理单击大红色 X 作为绕过表单上所有文本框验证的一种手段。
该代码将测试是否对表单上的数据绑定控件进行了任何更改。代码处理取消在关闭表单之前所做的更改。
还想取消点击大 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
我们尝试了这个,但是文本框控件的验证事件执行,这不是我们想要的。
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