Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. How can i intercept Escape button in dialog form?

How can i intercept Escape button in dialog form?

Scheduled Pinned Locked Moved Windows Forms
questioncsharpc++help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wolfgangpuelm
    wrote on last edited by
    #1

    Hi, I'm just moving from MFC to .NET. In MFC, I can easily intercept any message with PreTranslateMessage and do whatever i want with a particular msg. This is especially useful if a user presses the Escape button in a dialog. before all input is gone, i can ask the user if he/she really wants to proceed and discard all input. Should be no problem in .NET neither, i thought. But either it'S so obvious i just can't see it or it's not so easy after all(which would be a surprise since this is a frequently occurring scenario). What i do in respect to the Escape Button: it's assigned the DialogResult.Cancel value and the containing form sets this button's variable name as CancelButton. Any suggestions from someone? Thank's in advance! Wolfgang Puelm

    G M 2 Replies Last reply
    0
    • W wolfgangpuelm

      Hi, I'm just moving from MFC to .NET. In MFC, I can easily intercept any message with PreTranslateMessage and do whatever i want with a particular msg. This is especially useful if a user presses the Escape button in a dialog. before all input is gone, i can ask the user if he/she really wants to proceed and discard all input. Should be no problem in .NET neither, i thought. But either it'S so obvious i just can't see it or it's not so easy after all(which would be a surprise since this is a frequently occurring scenario). What i do in respect to the Escape Button: it's assigned the DialogResult.Cancel value and the containing form sets this button's variable name as CancelButton. Any suggestions from someone? Thank's in advance! Wolfgang Puelm

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      When you drop a button on the form, you can then set the CancelButton property of your form to that button. It means that when escape is pressed the event handler of that button will be invoked. The dialogresult for that button will be set to Cancel.

      #region signature my articles #endregion

      W 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        When you drop a button on the form, you can then set the CancelButton property of your form to that button. It means that when escape is pressed the event handler of that button will be invoked. The dialogresult for that button will be set to Cancel.

        #region signature my articles #endregion

        W Offline
        W Offline
        wolfgangpuelm
        wrote on last edited by
        #3

        Hi, thank you very much for your propmpt answer! But I'm afraid that's not my problem since the default event handler gives me no chance to remove this event (ESC pressed) from the form's message processing queue. That means the dialog will be inevitably be closed and that's what i'm trying to prevent if desired by user input. If this handler had a KeyEventArgs parameter instead of a EventArgs, the KeyEventArgs.Handled property could be set to true which might do the job (if it's passed by reference). Unfortunately, the event handler is implemented as it is. Once again, thank you for your effort! Regards Wolfgang Puelm

        1 Reply Last reply
        0
        • W wolfgangpuelm

          Hi, I'm just moving from MFC to .NET. In MFC, I can easily intercept any message with PreTranslateMessage and do whatever i want with a particular msg. This is especially useful if a user presses the Escape button in a dialog. before all input is gone, i can ask the user if he/she really wants to proceed and discard all input. Should be no problem in .NET neither, i thought. But either it'S so obvious i just can't see it or it's not so easy after all(which would be a surprise since this is a frequently occurring scenario). What i do in respect to the Escape Button: it's assigned the DialogResult.Cancel value and the containing form sets this button's variable name as CancelButton. Any suggestions from someone? Thank's in advance! Wolfgang Puelm

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          For this I would normally handle the FormClosing event. You'll then get the opportunity to find out why the form is closing by checking the DialogResult property. You should normally make the Cancel button, the Escape key, and the X button in the top-right corner all do the same thing. If you really want to go down the PreTranslateMessage route, you can implement the IMessageFilter interface and use Application.AddMessageFilter to have the framework call your filter function for each message.


          DoEvents: Generating unexpected recursion since 1991

          W 1 Reply Last reply
          0
          • M Mike Dimmick

            For this I would normally handle the FormClosing event. You'll then get the opportunity to find out why the form is closing by checking the DialogResult property. You should normally make the Cancel button, the Escape key, and the X button in the top-right corner all do the same thing. If you really want to go down the PreTranslateMessage route, you can implement the IMessageFilter interface and use Application.AddMessageFilter to have the framework call your filter function for each message.


            DoEvents: Generating unexpected recursion since 1991

            W Offline
            W Offline
            wolfgangpuelm
            wrote on last edited by
            #5

            Hi, Thanks a lot for your answer! It took me on the right track - at last: The xxx_FormClosing event has a FormClosingEventArgs parameter. From the MSDN i found out that there is a CancelEventArgs param also. Since it's possible to cast the FormClosingEventArgs into a CancelEventArgs, the latter's Cancel property can be used: if set true, the dialog stays alive, other it get's closed! :-D Thank's again! :rose: Regards. Wolfgang Puelm

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups