Mnemonic Shortcuts

J

Jo

Hi All,

In most new lanuages you put an & (ampersand) in a button's text and voila -
pressing ALT and the key after the & in the button text and the onclick
event fires.

C# supports this just fine BUT you can also just press the key without the
ALT button and it still fires the onlick event!
Is there any way one can prevent this second part from happening??? Maybe
one Global override function in a base class - I can override with the
ProcessMnemonic function but the I have to write one for each and every
button in the application - not what I wan't to do...

any ideas?
thanks
J
 
M

Michael Mayer

First of all, if it's default windows behavior, then "screwing" with that is
not a good idea, because then your app will not behave the way a user would
expect it to -aka java swing ;-).

If you insist on doing it (or have a REALLY strong reason to disable
functionality) you can probably "catch" all those non-alt key presses for
the whole form in one method:
Overide this method in the form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

If it is not an alt+key press, you can return true to "handle" the keypress
in this method (and the key press should not be sent to the control). If it
is an alt+key, you can go ahead and send the key press to the control.

I believe you have to set KeyPreview to true as well for the form.

I haven't tried that exact behaviour, but I have used it for similar things.
 
J

Jo

Thanks guys, will make a plan.


Michael Mayer said:
First of all, if it's default windows behavior, then "screwing" with that is
not a good idea, because then your app will not behave the way a user would
expect it to -aka java swing ;-).

If you insist on doing it (or have a REALLY strong reason to disable
functionality) you can probably "catch" all those non-alt key presses for
the whole form in one method:
Overide this method in the form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

If it is not an alt+key press, you can return true to "handle" the keypress
in this method (and the key press should not be sent to the control). If it
is an alt+key, you can go ahead and send the key press to the control.

I believe you have to set KeyPreview to true as well for the form.

I haven't tried that exact behaviour, but I have used it for similar things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top