防止VB.Net表单关闭

时间:2012-07-15 20:18:01

标签: 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

我们尝试了这个,但文本框控件的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

3 个答案:

答案 0 :(得分:11)

您根本不应该使用WndProc

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

答案 1 :(得分: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 = True”,表单永远不会关闭!

答案 2 :(得分: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以关闭表单,则不会验证。表单将显示消息,警告用户某些内容已更改,并让他们选择返回并尝试保存更改,或者只是退出而不保存任何内容。

相关问题