tags:

views:

257

answers:

4

HI, I have a form in C# app. On this form I capture a KeyDown event Alt+U which will open a second form. In the second form I have a toolStripButton with shortcutkey Alt+U (the same which I used to open the form with) which prints a document. Now, my problem is when I open the second form It will automatically trigger the event of clicking toolstripbutton since it has the same shortcutkey as I used to open the form with. How can I prevent this from happen.

Regards Johan

A: 

This doesn't answer your question, but you really should think about making two different shortcut keys for these two very different actions. Having two identical shortcut keys that do two entirely different actions is very confusing IMO.

To answer your question though, I would have some property on the second form like "ShouldRaise" or something, and only raise the Alt + U event in the second form if that flag is true. Set it to false initially, but then in the KeyUp in the first form, set it to true.

BFree
A: 

Would it not just be easier to change the shortcut of one to something else? I agree with BFree its not the best design have the same shortcut for 2 completely different functions. All shortcuts/accelerator keys should be unique.

Why not change the shortcut for the form page changing to something like:

Ctrl+Right (Go to next page) Ctrl+Left (Go to previous page)

James
A: 

On the second form do you actually have a ToolStripMenuItem instead? (a ToolStripButton doesn't have the ShortcutKeys property).

Do you instantiate a new form when the user presses Alt-U on the parent form?

Did you check the sender object on the handler that prints the document to see if it was the parent form?

Can't seem to reproduce your problem; a little explaining would help.

tzup
A: 

Another easy solution is to do some check of what form you are in from the event listener. You could just return inside the event handler inside your second form.

Again not the most elegant solution but should be a decent fix.

Mark