0

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.

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.

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.

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.

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
0

3 Answers 3

11

You shouldn't be using WndProc at all.

Instead, handle the FormClosing event and set e.Cancel to true.

2
  • 1
    Just tried it and the e.Cancel works nice but the textbox validation executes. Is there a way to cancel the textbox validation from executing in this scenario? Jul 15, 2012 at 20:28
  • 1
    I got it working with a combination of FormClosing and WndProc. Thanks everyone for your help. :-) Jul 15, 2012 at 23:10
2

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:

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!

2
  • Thanks for the reply. e.Cancel = True works great but now the Validating events for the textbox controls execute which is why we found that using WndProc() overrides the Validating events. Jul 15, 2012 at 20:39
  • You can still avoid WndProc() if you really want to. If you want to avoid validation events for the textbox to fire, you can set the textbox control's CausesValidation property to False. Jul 18, 2012 at 9:43
0

Thanks for letting me know about FormClosing and e.Cancel

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

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.

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:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.