title: Preventing a VB.Net form from closing
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Would would also like to cancel the clicking of the big X and not allow the form to close.</p>
  
  <p>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.</p>
  
  <pre><code>Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  
      Select Case ((m.WParam.ToInt64() And &amp;HFFFF) And &amp;HFFF0)
  
          Case &amp;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! " &amp; vbCrLf &amp; vbCrLf &amp; _
                                     "ARE YOU SURE YOU WANT TO DO THIS?" &amp; vbCrLf &amp; 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
  </code></pre>
  
  <p>We tried this but the Validating event of the textbox controls execute which is not what we want.</p>
  
  <pre><code>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! " &amp; vbCrLf &amp; vbCrLf &amp; _
                             "ARE YOU SURE YOU WANT TO DO THIS?" &amp; vbCrLf &amp; 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
  </code></pre>