According to this link, ...
http://support.microsoft.com/kb/146864
... VBA programs can trap user interrupts by setting the EnableCancelKey property to xlErrorHandler.
Is there a way to trap user interrupts in Visual Basic .Net? If no, why not?
Printable View
According to this link, ...
http://support.microsoft.com/kb/146864
... VBA programs can trap user interrupts by setting the EnableCancelKey property to xlErrorHandler.
Is there a way to trap user interrupts in Visual Basic .Net? If no, why not?
You're not exactly comparing apples to apples. What kind of user interrupts do you want to trap and what kind of work will your code be doing when the interrupts may happen?
I want to trap all user interrupts. In VBA, if you set the EnableCancelKey property to xlErrorHandler, "all interrupts will generate a run-time error number 18".
What kind of work will your code be doing when the interrupts may happen I'm not following you. Can't an interrupt happen at any point in the program?
The way that vb.net handles errors is with a Try..Catch block:
If you are worried about something being unhandled in your app then you can use the Application.Designer.vb. To get to this, make sure you are looking at ALL files (button is located at the top of the solution explorer).Code:Try
'......do code
Catch Sqlex as SqlClient.SqlException
'You can specify specific catches based on exception types OR
Catch ex as Exception
'This will catch ALL exceptions.
Finally
'This will trigger irregardless of error or no error (unless total application failure).
'You can use this to clean up database connections or objects, etc.
End Try
If you want to just clean up any stray items in your program before closing then you can use this, also located in the Application.Designer.vb:Code:Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
'You can put specific program related options in here
End Sub
I hope this helps to answer your question(s). :wave:Code:Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
D
Thank you :wave: . I know about Try...Catch blocks. You use them to catch exceptions.
However, I want to catch user interrupts.
"A user can interrupt a Visual Basic for Applications procedure by pressing CTRL+BREAK or ESC (COMMAND+PERIOD on the Macintosh)."
Pressing these buttons has no effect on my Visual Basic .Net programs (I'm using 2005) :confused: .
That's why I asked the above questions. You will have to create the behaviors based on what you expect to happen.Quote:
Originally Posted by esharris
You have to/can program them in....
There are a couple of methods to this. Keypreview on a form then catch the forms KeyDown event. Test for your "break" and do your code.
The other way is to craete a menu with a menu item and set the menu item's shortcut key to Ctrl-Break. You can then hide that menu item so the user will not see it. Then in the menu items code you can do whatever you need to.
Good Luck!! :wave:
D
VB.NET isn't a macro language designed to work with a specific application framework like Word or Excel like VBA is designed to do. It's a general purpose programming language. If you want these kind of user interrupts you have to code them into your application yourself. You would probably code it by either using standard form events or by hooking into Windows messages if you wanted a more generalized approach.Quote:
Originally Posted by esharris
Please forgive my ignorance :blush: .
Isn't the handling of form events serialized? Suppose I trigger a form event that kicks off a time consuming subroutine. Doesn't VB .Net wait for the current event to complete before handling a new event? If this is true, I can't write a keydown event to interrupt the processing of time-consuming event. Right?
No, but you can use threading to handle the time-consuming event, and then interrupt the thread on user input. Look at the BackgroundWorker.
The BackgroundWorker looks like a solution. Thanks!