Connecting Tech Pros Worldwide Forums | Help | Site Map

Sending events to parent form even when a dialogue is open

Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#1: Dec 31 '08
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.

Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#2: Dec 31 '08

re: Sending events to parent form even when a dialogue is open


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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#3: Dec 31 '08

re: Sending events to parent form even when a dialogue is open


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.
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#4: Dec 31 '08

re: Sending events to parent form even when a dialogue is open


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.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#5: Dec 31 '08

re: Sending events to parent form even when a dialogue is open


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

Something like:
Expand|Select|Wrap|Line Numbers
  1. mydialogform d = new mydialogform();
  2. d.KeyPress+=new KeyPressEventHandler(d_KeyPress);
  3. d.ShowDialog();
  4.  
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.
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#6: Apr 22 '09

re: Sending events to parent form even when a dialogue is open


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?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Apr 22 '09

re: Sending events to parent form even when a dialogue is open


You could try using "global hooks":
http://www.codeproject.com/KB/cs/globalhook.aspx

You would not need to attach event handlers everywhere then
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#8: Apr 22 '09

re: Sending events to parent form even when a dialogue is open


Thanks a lot,

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

Thanks
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#9: Apr 27 '09

re: Sending events to parent form even when a dialogue is open


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:
http://www.codeproject.com/KB/cs/globalhook.aspx

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
Reply

Tags
dialogue, events, keypress, parent, show