简体   繁体   English

防止关闭VB.Net表单

[英]Preventing a VB.Net form from closing

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

You shouldn't be using WndProc at all. 您根本不应该使用WndProc

Instead, handle the FormClosing event and set e.Cancel to true. 而是,处理FormClosing事件并将e.Cancel设置为true。

As SLaks said, you should use the FormClosing() event procedure instead of introducing complexity with WndPorc(). 正如SLaks所说,您应该使用FormClosing()事件过程,而不要在WndPorc()中引入复杂性。 Overriding WndProc() is used in languages like C++ where you don't have the luxury of an event procedure to handle these events. 覆盖WndProc()用于C ++之类的语言,在这些语言中,没有足够的事件过程来处理这些事件。 But the simplicity of VB.NET provides you with an event procedure called FormClosing(). 但是VB.NET的简单性为您提供了一个称为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). 只需打开代码,然后在对象下拉菜单(左侧)中选择表单名称,然后从事件下拉菜单(右侧)中选择FormClosing。 This should give you a template to write your event code, something like this: 这应该为您提供一个模板来编写事件代码,如下所示:

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”,该表格将永远不会关闭!

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. 我的WndProc现在看起来像这样。 Notice the setting of blneCancel. 注意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. 现在,用户可以在电话号码文本框中键入任何内容,并且如果用户单击大X来关闭表单,它将无法验证。 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. 表单将仅显示警告用户的信息,告知用户某些内容已更改,并让他们选择返回并尝试保存更改或退出而不保存任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM