Results 1 to 11 of 11

Thread: Form_QueryUnload in vb.net

  1. #1

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Form_QueryUnload in vb.net

    i want to my application from closing when the user presses the "X" button on the form, what is the event for that
    Women ...r like tea bags, you neva know how strong they really r untill u put them in hot water

    Huzefa Yousuf
    Software Engineer
    Verticity Inc.
    +92-345-2235303

    [email protected]

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form_QueryUnload in vb.net

    I think you mistyped. Are you saying you want to stop your app from closing if the user clicks the Close button in the title bar? If so, you need to set a variable when the user selects a legal closing method. You then check that variable in the form's Closing event handler. If the variable is set then you do nothing and the app closes. If the variable is not set, set the Cancel property of the CancelEventArgs argument to True and the app will not close.

  3. #3

    Thread Starter
    Addicted Member hyousuf2's Avatar
    Join Date
    Dec 2004
    Location
    Dublin
    Posts
    226

    Re: Form_QueryUnload in vb.net

    i found it myself... heres the code

    VB Code:
    1. Const WM_ENDSESSION As Integer = &H16
    2.     Dim osexit As Boolean = False
    3.  
    4.     Protected Overrides Sub WndProc(ByRef e As Message)
    5.         If (e.Msg = WM_ENDSESSION) Then
    6.             osexit = True
    7.             Application.Exit()
    8.         End If
    9.         MyBase.WndProc(e)
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    14.  
    15.         If osexit Then
    16.             'Not doing anything will let the application close
    17.         Else
    18.             Dim sure
    19.             sure = MsgBox("Are you sure you want to terminate the application", MsgBoxStyle.Critical + MsgBoxStyle.YesNo)
    20.             If sure = vbYes Then
    21.                 Application.Exit()
    22.             Else
    23.                 e.Cancel = True 'This will cause the application to ignore the close & continue running
    24.                 Me.Hide()
    25.             End If
    26.         End If
    27.     End Sub
    Women ...r like tea bags, you neva know how strong they really r untill u put them in hot water

    Huzefa Yousuf
    Software Engineer
    Verticity Inc.
    +92-345-2235303

    [email protected]

  4. #4
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Form_QueryUnload in vb.net

    Why have you over-ridden the wndProc procedure? Could you care to explain?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form_QueryUnload in vb.net

    When you call Application.Exit the app shuts down without raising any Form.Closing events, so setting osexit and then testing for it in the Closing event handler is pointless. Whenever it is set to true the Closing event handler will not be called anyway.

  6. #6
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Form_QueryUnload in vb.net

    There is one more gap.
    I ran the application. I shut down windows. The application prompted me for exit. I clicked on No. The windows shut-down process was aborted, but the application got closed. Could you tell me why is that happening?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form_QueryUnload in vb.net

    Quote Originally Posted by abhijit
    There is one more gap.
    I ran the application. I shut down windows. The application prompted me for exit. I clicked on No. The windows shut-down process was aborted, but the application got closed. Could you tell me why is that happening?
    My guess is that the form Closing event handler is called, you are prompted, you say no so the app shutdown is aborted and thus the Windows shutdown is aborted. Then the WM_ENDSESSION message is processed and Application.Exit is called, which exits the app without raising the Closing event and thus you don't get prompted. I don't know that this is what happens for a fact, but it seems likely. hyuosuf2, what exactly is it that you are trying to achieve and we may be able to find a bug free way to do it? It is not clear from your original post or your code.

  8. #8
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Form_QueryUnload in vb.net

    I tried putting some code over here. For windows shut-down, but that did not work.

    VB Code:
    1. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.  
    3.         If osexit Then
    4.             'Not doing anything will let the application close
    5.             e.Cancel = True 'A windows shutdown will not shut down the app.
    6.  
    7.         Else
    8.             Dim sure
    9.             sure = MsgBox("Are you sure you want to terminate the application", MsgBoxStyle.Critical + MsgBoxStyle.YesNo)
    10.             If sure = vbYes Then
    11.                 Application.Exit()
    12.             Else
    13.                 e.Cancel = True 'This will cause the application to ignore the close & continue running
    14.                 'Me.Hide()
    15.             End If
    16.         End If
    17.     End Sub

    Help,
    Abhijit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form_QueryUnload in vb.net

    Quote Originally Posted by abhijit
    I tried putting some code over here. For windows shut-down, but that did not work.

    VB Code:
    1. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.  
    3.         If osexit Then
    4.             'Not doing anything will let the application close
    5.             e.Cancel = True 'A windows shutdown will not shut down the app.
    6.  
    7.         Else
    8.             Dim sure
    9.             sure = MsgBox("Are you sure you want to terminate the application", MsgBoxStyle.Critical + MsgBoxStyle.YesNo)
    10.             If sure = vbYes Then
    11.                 Application.Exit()
    12.             Else
    13.                 e.Cancel = True 'This will cause the application to ignore the close & continue running
    14.                 'Me.Hide()
    15.             End If
    16.         End If
    17.     End Sub

    Help,
    Abhijit
    Like I said, that code will never be executed. The only place that osexit gets set to True calls Application.Exit on the next line. Application.Exit shuts down the app without raising any Form.Closing events, so the event handler containing your new bit of code is not called.

  10. #10
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: Form_QueryUnload in vb.net

    So in that case, how do u prevent your application from closing up?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form_QueryUnload in vb.net

    Quote Originally Posted by abhijit
    So in that case, how do u prevent your application from closing up?
    Remove the call to Application.Exit(). Note that once you have tested osexit and found it to be True, you must then set it back to False or you will not be prompted on any subsequent attempts to close, i.e.
    VB Code:
    1. If osexit Then
    2.     e.Cancel = True
    3.     osexit = False

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