防止VB.Net表单关闭

Ema*_*een 0 vb.net wndproc

我们正在使用此编码来处理大红色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
Run Code Online (Sandbox Code Playgroud)

我们尝试了这个,但文本框控件的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
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 11

你根本不应该使用WndProc.

相反,处理FormClosing事件并设置e.Cancel为true.