Terminating Windows forms using the X

Hi,
I have a question about terminating forms and message boxes when using VB6 .  Its about closing the form by clicking on the X in the far right corner.  Basically before the form closes down I wish for a message box to appear that has a yes or no option.  If yes is clicked then it returns to the form and does not close down and if no is clicked then it continues to close. 

I have some very basic code in Form_Terminate() subrountine.  However this closes down the form and then shows the message box once the form has already closed.  

Does anybody know what I am doing wrong
  


Answer this question

Terminating Windows forms using the X

  • Sanjiro

    So basically you want to make SURE they want to exit
    Here's what I do:



      protected override void OnClosing(CancelEventArgs cea) {
        DialogResult dr = MessageBox.Show("Are you sure you want to exit ", "Leaving so soon ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
          if (dr==DialogResult.Yes) {
                Application.Exit();
          } else {
                cea.Cancel = true;
          }
      }



    Just overrides the form's onclosing event.
    Now let's see if I can remember VB.NET and convert... here goes:



      Protected Overrides Sub OnClosing(ByVal cea As CancelEventArgs) 
        DialogResult dr = MessageBox.Show("Are you sure you want to exit ", "Leaving so soon ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

          If dr = DialogResult.Yes Then
                Application.Exit()
          Else
                cea.Cancel = True
          End If
      End Sub



    Ok, I think my vb is right... I KNOW the C# is.
    Hope this helps!

  • Nadeem_IQBAL_NL

    Firstly, this forum is for .NET technologies of which VB6 is not a member of.

    Secondly, you should use the QueryUnload Event of your form.  It is "cancellable."

  • Terminating Windows forms using the X