Results 1 to 11 of 11

Thread: Trap user interrupts in Visual Basic .Net?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    56

    Question Trap user interrupts in Visual Basic .Net?

    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?

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Trap user interrupts in Visual Basic .Net?

    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?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    56

    Re: Trap user interrupts in Visual Basic .Net?

    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?
    Last edited by esharris; May 31st, 2007 at 07:58 AM.

  4. #4
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Trap user interrupts in Visual Basic .Net?

    The way that vb.net handles errors is with a Try..Catch block:
    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 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:
    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
    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_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
    
    End Sub
    I hope this helps to answer your question(s).

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    56

    Re: Trap user interrupts in Visual Basic .Net?

    Thank you . 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) .

  6. #6
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Trap user interrupts in Visual Basic .Net?

    Quote Originally Posted by esharris
    Pressing these buttons has no effect on my Visual Basic .Net programs (I'm using 2005) .
    That's why I asked the above questions. You will have to create the behaviors based on what you expect to happen.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  7. #7
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Trap user interrupts in Visual Basic .Net?

    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!!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  8. #8
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: Trap user interrupts in Visual Basic .Net?

    Quote Originally Posted by esharris
    Thank you . 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) .
    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    56

    Re: Trap user interrupts in Visual Basic .Net?

    Please forgive my ignorance .

    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?

  10. #10
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Trap user interrupts in Visual Basic .Net?

    No, but you can use threading to handle the time-consuming event, and then interrupt the thread on user input. Look at the BackgroundWorker.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    56

    Re: Trap user interrupts in Visual Basic .Net?

    The BackgroundWorker looks like a solution. Thanks!

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