防止 VB.Net 表单关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11495284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:12:37  来源:igfitidea点击:

Preventing a VB.Net form from closing

vb.netwndproc

提问by Emad-ud-deen

We are using this coding to handle the clicking of the big red X as a means to bypass all textbox validation on the form.

我们正在使用这种编码来处理点击大红色 X 作为绕过表单上所有文本框验证的手段。

The code will test if any changes are made to the data bound controls on the form. The code handle cancelling changes made prior to closing the form.

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

Would would also like to cancel the clicking of the big X and not allow the form to close.

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

Can you show any needed coding that will not allow the form to actually close? We would like to add this new coding after the Else statement in the coding show below.

您能否显示任何不允许表单实际关闭的所需编码?我们想在下面的编码展示中的 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

We tried this but the Validating event of the textbox controls execute which is not what we want.

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

回答by SLaks

You shouldn't be using WndProcat all.

你根本不应该使用WndProc

Instead, handle the FormClosingevent and set e.Cancelto true.

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

回答by Prahlad Yeri

As SLaks said, you should use the FormClosing() event procedure instead of introducing complexity with WndPorc(). Overriding WndProc() is used in languages like C++ where you don't have the luxury of an event procedure to handle these events. But the simplicity of VB.NET provides you with an event procedure called FormClosing(). Just open your code and select your form name in the object dropdown (on left), and select FormClosing from the events dropdown (on right). This should give you a template to write your event code, something like this:

正如 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

Just add "e.Cancel = True" as shown above, and the form will never close!

只需添加如上所示的“e.Cancel = True”,表单将永远不会关闭!

回答by Emad-ud-deen

Thanks for letting me know about FormClosing and e.Cancel

感谢您让我了解 FormClosing 和 e.Cancel

I was able use a combination of FormClosing and WndProc that handles everything we need.

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

I added this right after the form class name:

我在表单类名之后添加了这个:

Dim blneCancel As Boolean = False

My WndProc now looks like this. Notice the setting of blneCancel.

我的 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

The FormClosing procedure looks like this:

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

Now the user can type anything in the phone number textbox and it won't validate if the user clicks the big X to close the form. The form will just show the message warning the user something has changed and give them the choice to get back and attempt to save the changes or just exit without saving anything.

现在,用户可以在电话号码文本框中键入任何内容,并且不会验证用户是否单击大 X 以关闭表单。该表单将仅显示警告用户某些内容已更改的消息,并让他们选择返回并尝试保存更改或直接退出而不保存任何内容。