RyanB88

I need to find out how to add a confirmation dialog box if the application is going to exit, ie i call application.exit() or the last form is being closed. I need my app to confirm the exit with the user and also perform some maintenance type tasks before it exists as well.


Re: Visual Basic Language Execute Code on Application Exit

Dave299

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

If MessageBox.Show("Do you really want to quit", "End Program ", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then

e.Cancel = True

Else

' Do other things before exiting

End If

End Sub





Re: Visual Basic Language Execute Code on Application Exit

RyanB88

the code works, it just doesn't do what i desire. That is code that executes when the form is closed, I need something that will execute when the application exits, which is when the last form closes. I think I found part of the answer in ApplicationEvents.vb

Namespace My

' The following events are availble for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication

End Class

End Namespace

so as the remarks state, i need to use the shutdown event, I just need to find out how now.




Re: Visual Basic Language Execute Code on Application Exit

ahmedilyas

why not implement the ApplicationExit event its in the Application class

private sub Application_ApplicationExit(byval sender as object, byval e as eventargs) 

   'do your thing

end sub

 

'somewhere add the handler, perhaps in the form close event

addhandler Application.ApplicationExit, addressOf Application_ApplicationExit

 

however you may have problems if you are trying to access objects as it may already been disposed of, since this is the last step/event that gets fired when the app is almost ready to close






Re: Visual Basic Language Execute Code on Application Exit

RyanB88

what are you even talking about

all i need to know is what this error means:

Error 1 Method 'Private Sub Application_Exit()' cannot handle Event 'Public Event Shutdown(sender As Object, e As System.EventArgs)' because they do not have the same signature. C:\App\ApplicationEvents.vb 11 51 App

for this code

Partial Friend Class MyApplication
Private Sub Application_Exit() Handles Me.Shutdown

End Sub
End Class

in ApplicationEvents.vb





Re: Visual Basic Language Execute Code on Application Exit

RyanB88

I figured it out, i need that code to be this instead:

Partial Friend Class MyApplication
Public Sub App_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

End Sub
End Class




Re: Visual Basic Language Execute Code on Application Exit

ahmedilyas

you didnt read the previous code correctly ;-)

But I see what you mean with that.

take a look at this:

http://msdn2.microsoft.com/en-us/library/036kk0as.aspx

I believe the ShutDown is the same thing as the Application exit event. It's just a shortcut via the my namespace

in regards to the error, you need to declare it like the code posted previously






Re: Visual Basic Language Execute Code on Application Exit

RyanB88

the problem with my code i just noticed is i can't use e.cancel() to exit the shutdown procedure if they cancel it via the message box, their is not cancel option for e. How do i stop the application from exiting then




Re: Visual Basic Language Execute Code on Application Exit

ahmedilyas

well this is done, as suggest previously, from the Form_Closing event. This is where you can confirm cancellation of the application or cancel the request and carry on.

have you tried to implement the Application_Exit event (commenting out the My.Shutdown event) and seeing if this will meet your requirements






Re: Visual Basic Language Execute Code on Application Exit

RyanB88

what

I already got my code to handle the shutdown of the application, I need to know how to abort this shutdown, it must be possible, this is ran prior to the shutdown process.





Re: Visual Basic Language Execute Code on Application Exit

ahmedilyas

so basically you want to cancel a shut down event AFTER confirming shutdown of the application by the user is this correct




Re: Visual Basic Language Execute Code on Application Exit

Dave299

If you call Application.Exit then you should easily be able to code up a confirmation around this and not call it if the user decides not to quit.

If the user is closing forms using their close button then the following code will give you the result you are after if you put the same code in all of your forms.

Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

If My.Application.OpenForms.Count > 1 Then

' do nothing

Else

If MessageBox.Show("Do you really want to quit", "End Program ", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then

e.Cancel = True

Else

' Do other things before exiting

End If

End If

End Sub





Re: Visual Basic Language Execute Code on Application Exit

RyanB88

is that all i only have to put that in all 50 forms, well thats a whole lot easier than finding out what line or few lines of code it will take to abort the shutdown in the code i already have. Not too mention your method is too redundant, which beyond making the application larger makes it harder to update the shutdown process for future versions.




Re: Visual Basic Language Execute Code on Application Exit

helios456

Ryan, you're a sarcastic *** and I don't know why anyone should help you. But...

There's a way that you would be able to do it without the 50 redundant methods.

Just have your own custom form that inherits from Form and put the code in there.

Then have all your forms inherit from that one.

Now if you have another sarcastic comment about that one, I'd like to hear it.