Accelerator keys

A

Alex

I have .Net windows forms with buttons. Each button has been assigned
with accelerator key. But once form opens the accelerator keys works
even if the ALT key is not pressed. For example for Button "&Save"
pressing the S key without ALT fires the button click.
Is there any way to supress this behaviour? Or this is standard for
Windows Forms?
 
S

Scott Seligman

Alex said:
I have .Net windows forms with buttons. Each button has been assigned
with accelerator key. But once form opens the accelerator keys works
even if the ALT key is not pressed. For example for Button "&Save"
pressing the S key without ALT fires the button click.
Is there any way to supress this behaviour? Or this is standard for
Windows Forms?

It's standard for Windows dialogs.
 
J

Jeff Johnson

I have .Net windows forms with buttons. Each button has been assigned
with accelerator key. But once form opens the accelerator keys works
even if the ALT key is not pressed. For example for Button "&Save"
pressing the S key without ALT fires the button click.
Is there any way to supress this behaviour? Or this is standard for
Windows Forms?

This will work if you have multiple buttons on the form and one of those
buttons has keyboard focus. Technically Windows has the concept of "groups,"
which button controls can be in (and remember, radio buttons and check boxes
are also buttons). The idea is to provide easy keyboard access to this group
of buttons, allowing you to, for example, simply hit Y or N in a Yes/No
dialog box.

Groups are defined by setting the WS_GROUP style on the FIRST item in the
group. The group then continues until the next control that has the WS_GROUP
style, which starts a new group. The accelerators only work within a group.
Here's the MSDN definition of the style:

################
WS_GROUP
Specifies the first control of a group of controls. The group consists of
this first control and all controls defined after it, up to the next control
with the WS_GROUP style. The first control in each group usually has the
WS_TABSTOP style so that the user can move from group to group. The user can
subsequently change the keyboard focus from one control in the group to the
next control in the group by using the direction keys.
You can turn this style on and off to change dialog box navigation. To
change this style after a window has been created, use SetWindowLong.
################

Note that the description above talks about arrow keys but fails to mention
accelerators. Good job, MSDN.

Now, here's the trick: for some reason, Microsoft seems to have decided that
this particular style isn't important enough to expose to us lowly non-C[++]
developers, because there is no easy way to set it in Windows Forms, nor was
it ever available in VB. You have to call SetWindowLong() directly to fiddle
with it. So as long as your buttons are all in the same container, you can
control grouping without P/Invoke. If it's really bugging you and you don't
want to call Windows API functions, you could put each button in a
borderless group box.
 

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