Pregunta

I want to add shortcut key to checkbox. Checkbox do not have text. I have label and then Checkbox. Label have shortcut key for ex. &Visible. So, Label have V as shortcut key. If someone press Alt+V then chechbox should change from selected to not selected state and same in opposite manner.

¿Fue útil?

Solución

Label controls are special with respect to keyboard mnemonics. Since labels can't ever get the focus, whenever you attempt to set the focus to a label using its keyboard mnemonic, the label sets the focus to the very next control in the tab order.

This is intended for use with textboxes and comboboxes, which don't have any built-in facility for displaying a label (in contrast to the check box and option button controls). To set up a mnemonic for these controls, you position a label next to them, set a mnemonic for it, and ensure that it comes right before the textbox in the tab order. That way, when the user activates the keyboard mnemonic for the label, it automatically sets focus to the textbox control. You've seen this all over the place in Windows:

     example of textbox with a label used as the mnemonic

Well, you can do exactly the same thing with a checkbox control if you must (though I'm really not sure why you'd want to). Set the mnemonic for the label (&Visible), and then position the label next to the checkbox that you want it to work with. Use the TabIndex configuration options in the Visual Studio IDE to ensure that if the label has tab index n, the checkbox control has tab index n+1.

There's no need to override ProcessCmdKey or anything else difficult.

Otros consejos

You can check it like this refer the following code part.

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // look for the expected key 
            if (keyData == Keys.Alt && keyData == Keys.V)
            {
                checkBox1.Checked = true;
                return true;
            }
            else
            {
                checkBox1.Checked = false;
                return false;
            }
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top