Sending events to parent form even when a dialogue is open

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harvindersingh
    New Member
    • Sep 2008
    • 24

    Sending events to parent form even when a dialogue is open

    Hello guys

    I have some custom events in my application i.e. KeyPress such as full screen for pressing F11 and so on. Currently this works fine but only when the main form which is listening tot he KayPress events. I would like this to work even when a Dialogue window is open from this form for example I am opening a child window with Show.Dialogue() . This is blocking the events from going to the parent form.

    Can someone please put me in the right direction or give some hints on this.

    Thanks in advance.
  • harvindersingh
    New Member
    • Sep 2008
    • 24

    #2
    sorry guys I was meant to type:

    ShowDialog();

    in my above post. This is what I am using to show the form as a dialog.

    thanks

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Well the point of a dialog is to force users to only be able to interact with THAT form, otherwise you would just use .Show()

      You could consider capturing events on that Dialog and then pipe them back to the parent window.

      Comment

      • harvindersingh
        New Member
        • Sep 2008
        • 24

        #4
        This sounds okay to me, but how would I send the same event back tot he paren form? i.e. lets say I have this KeyDown event of F11 how would I send it back to parent form when the Dialog is open?

        help appreciated.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          In your case it might be rather easy.
          Attach an event handler to the dialog form in the parent form.

          Something like:
          [code=c#]
          mydialogform d = new mydialogform();
          d.KeyPress+=new KeyPressEventHa ndler(d_KeyPres s);
          d.ShowDialog();
          [/code]
          Then in your d_keypress function you can perform the same checks for F11(and etc)

          Actually, you might be able to RE-USE the same keypress event handler you have for the parent form, by also attaching it to dialogform.

          Comment

          • harvindersingh
            New Member
            • Sep 2008
            • 24

            #6
            Hello

            I know this an old post, but rather than creating a new thread I think its better to extend this one as the question is relted to the above subject as well.

            The above mentioned method to pipe through the KeyPress events to the Parent works like a charm, but it gets rather painful when there are a lot of Dialogue forms in the application. I was wondering if there is a way to capture events globally within the application, such that the keypress events are also forwarded to the Parent form. Reason for having such a global control is that I will be having Dialogues generated by the Dialogues of the Parent form i.e. Dialogue in another layer of Dialogue like a grand child. It makes the events piping a little messy as I would have to attach listner for each instance.

            I have searched this forum for an answer to my question but I could only find something that is related to global events listner from CodeProject: http://www.codeproject.com/KB/cs/globalhook.aspx

            I think it only send events to the application as a whole which means the events will go to the currently active Dialgue/Form and the parent Form in this case may not be the active Form. So is there any solution to my problem?

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              You could try using "global hooks":


              You would not need to attach event handlers everywhere then

              Comment

              • harvindersingh
                New Member
                • Sep 2008
                • 24

                #8
                Thanks a lot,

                Sorry I posted before testing the application, the project on CodeProject does provide the solution to my problem.

                Thanks

                Comment

                • harvindersingh
                  New Member
                  • Sep 2008
                  • 24

                  #9
                  Hello

                  I have been thinking of finding a slightly different way of finding a way to send those KeyDown events to the parent Form rather than using that "global hooks" mecahnism from codeproject:


                  I tried to create a custom events using Delegate, and crated and listener for it in the parent Form, a method to invoke the event is created in the sub child Form or any other Form. When the event is knvoked I can see that the Parent Form can listen to the event, I am also able to send KeyPressArgs in the events to find which key was pressed.

                  However this custom event I build will be resideing in a Static class, so that it is visible to all Forms in the application. I was wondering if this is a good way to go about my problem? as in it wouldn't effect or create a new problem for me? I would very much appreciate your view on this, as I do not have a lot of experience with C#.

                  Thanks

                  Comment

                  Working...