locked
Confirmation dialog box at FormClosing problem RRS feed

  • Question

  • My FormClosing event code of Form1 is the following:

    Private Sub form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Dim Result As DialogResult
            Result = MsgBox("Are you sure you want to exit?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo)
            If Result = System.Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
    End Sub

    But in a button click event I wrote the following code:

    form2.Show()
    Me.Close()

    This causes to show the confirmation dialog box every time I click the button to load form2. But I want the dialog box to appear only when I click the cross button in the title bar. How do I stop displaying the dialog box when loading another form?


    -k@N@k-

    Monday, July 23, 2012 10:51 AM

Answers

  • At class level:

        private _CloseByCode as boolean

    Later..:

      _CloseByCode  = True
      Me.Close()
      _CloseByCode  = False

    Start of FormClosing:

      if _CloseByCode then return


    Armin

    • Proposed as answer by Rudedog2 Monday, July 23, 2012 6:58 PM
    • Marked as answer by Youen Zen Friday, August 3, 2012 9:00 AM
    Monday, July 23, 2012 6:25 PM

All replies

  • Don't close it, Hide it:  Me.Hide()
    Monday, July 23, 2012 10:55 AM
  • Don't close it, Hide it:  Me.Hide()
    I have got a lot of forms in my application all of which loads a lot of data. So I am going to have some memory issue if I use Me.Hide().

    -k@N@k-

    Monday, July 23, 2012 11:03 AM
  • Don't close it, Hide it:  Me.Hide()
    I have got a lot of forms in my application all of which loads a lot of data. So I am going to have some memory issue if I use Me.Hide().

    -k@N@k-


    Then  use a boolean to tell the close code what to do.  Set it or clear it from your other code.
    Monday, July 23, 2012 11:06 AM
  • Don't close it, Hide it:  Me.Hide()
    I have got a lot of forms in my application all of which loads a lot of data. So I am going to have some memory issue if I use Me.Hide().

    -k@N@k-


    Then  use a boolean to tell the close code what to do.  Set it or clear it from your other code.
    How exactly do I do that? I mean how do I pass the parameters? Can you give me an example code?

    -k@N@k-

    Monday, July 23, 2012 11:11 AM
  • At class level:

        private _CloseByCode as boolean

    Later..:

      _CloseByCode  = True
      Me.Close()
      _CloseByCode  = False

    Start of FormClosing:

      if _CloseByCode then return


    Armin

    • Proposed as answer by Rudedog2 Monday, July 23, 2012 6:58 PM
    • Marked as answer by Youen Zen Friday, August 3, 2012 9:00 AM
    Monday, July 23, 2012 6:25 PM
  • At class level:

        private _CloseByCode as boolean

    Later..:

      _CloseByCode  = True
      Me.Close()
      _CloseByCode  = False

    Start of FormClosing:

      if _CloseByCode then return


    Armin

    I'd put that code in the override of the OnFormClosing event, and then set e.Cancel to true inside of the override.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Monday, July 23, 2012 7:02 PM
  • If you make the distinction between FormClosing and OnFormClosing, you shouldn't call the latter an event. ;-) e.cancel=true was already there in the original code. But of course you're right.

    Armin

    Monday, July 23, 2012 7:04 PM
  • If you make the distinction between FormClosing and OnFormClosing, you shouldn't call the latter an event. ;-) e.cancel=true was already there in the original code. But of course you're right.

    Armin

    It's only semantics but it does help to understand what delegates and events are.  You can override the OnFormClosing method.  The FormClosing event is raised by the OnFormClosing method.  Within a class, it's probably preferable to override the method rather than subscribe to the event.

    • Edited by JohnWein Monday, July 23, 2012 7:36 PM
    Monday, July 23, 2012 7:34 PM