Results 1 to 4 of 4

Thread: [RESOLVED] How to stop closing form?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    110

    Resolved [RESOLVED] How to stop closing form?

    I need to check whether the user really want to close the form.

    But if I do this in Formclosing, the form will be closed anyway.

  2. #2
    Addicted Member
    Join Date
    May 2008
    Location
    Denmark
    Posts
    178

    Re: How to stop closing form?

    You can use e.Cancel to close or cancel closing the form.

    vb.net Code:
    1. Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.         If MsgBox("Are you sure you want to close?", MsgBoxStyle.YesNo, "Close?") = MsgBoxResult.Yes Then
    3.             e.Cancel = False
    4.         Else
    5.             e.Cancel = True
    6.         End If
    7.     End Sub

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Cool Re: How to stop closing form?

    This question has already been answered nicely by Link, though this sample code uses the preferred .Net framework System.Windows.Forms namespace version of the message box as opposed to the Microsoft.VisualBasic.Interaction namespace version:
    Code:
        Private Sub Form1_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Select Case MessageBox.Show("Are you sure you want to close?", "Confirm form closure", _
                                         MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            Case Windows.Forms.DialogResult.Yes
                e.Cancel = False
            Case Else
                e.Cancel = True
            End Select
        End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: How to stop closing form?

    To summarize it all on one line:
    Code:
    Private Sub Form1_FormClosing(...) Handles Me.FormClosing
        e.Cancel = MessageBox.Show("Are you sure you want to close?", "Confirm form closure", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes
    End Sub
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width