0

I have a small form with 9 Check Boxes in it. I am trying to make hotkeys for those boxes that correspond with the Numpad, but I'm having the darnedest time. I have two main problems:

1.

 private void checkBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.NumPad7)
        {
            MessageBox.Show("It's working");

        }
    }

That is my code. It works, but doesn't do what I want. It makes a message appear, but ONLY if that checkbox is highlighted. I think KeyPreview might help in this context, but the MSDN database didn't help me solve my problem with trying to figure out how to make KeyPreview work.

Second, I want the code to check the box when I hit the hotkey. No combination I can figure using CheckState seems to work. If anyone has some incite, I would greatly appreciate it.


Code from Comments:

public Form2() 
{ 
    InitializeComponent(); 
    this.KeyPreview = true; 
    this.KeyDown += new KeyEventHandler(Form2_KeyDown);
} 

private void Form2_KeyDown(object sender, KeyEventArgs e) 
{ 
    switch (e.KeyCode) 
    { 
        case Keys.NumPad7: 
            MessageBox.Show("ABC"); 
            break; 

        default: 
            break; 
    } 
} 
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
Tim
  • 23
  • 1
  • 1
  • 6
  • http://stackoverflow.com/q/4378865 – Robert Harvey Mar 17 '13 at 23:05
  • You're starting to attract answers that don't make much sense. Tell us a little more about the behavior you want. Do you want to assign each number key to a checkbox on the form? – Robert Harvey Mar 17 '13 at 23:08
  • 1
    I'm sorry. I tried to make it as clear as possible. I want NumPad to correspond to the 9 checkboxes, so that hitting them will check and uncheck the corresponding box. – Tim Mar 18 '13 at 00:57

3 Answers3

1

Your question does not state wether or not you want this to be an application specific hotkey or a system hotkey. I am operating under the assumption that it is an Application specific one. You were right in your assumption that you need to set the Forms KeyPreview Property to true. You then need to put your code for setting and clearing your checkbox's in the Forms Keydown event like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode )
    {
        case Keys.NumPad1:
            if (checkBox1.Checked)
                checkBox1.Checked = false;
            else
                checkBox1.Checked = true;

            break;
        case Keys.NumPad2:
            if (checkBox2.Checked)
                checkBox2.Checked = false;
            else
                checkBox2.Checked = true;

            break;
        case Keys.NumPad3:
            if (checkBox3.Checked)
                checkBox3.Checked = false;
            else
                checkBox3.Checked = true;

            break;
        case Keys.NumPad4:
            if (checkBox4.Checked)
                checkBox4.Checked = false;
            else
                checkBox4.Checked = true;

            break;
        case Keys.NumPad5:
            if (checkBox5.Checked)
                checkBox5.Checked = false;
            else
                checkBox5.Checked = true;

            break;
        case Keys.NumPad6:
            if (checkBox6.Checked)
                checkBox6.Checked = false;
            else
                checkBox6.Checked = true;

            break;
        case Keys.NumPad7:
            if (checkBox7.Checked)
                checkBox7.Checked = false;
            else
                checkBox7.Checked = true;

            break;
        case Keys.NumPad8:
            if (checkBox8.Checked)
                checkBox8.Checked = false;
            else
                checkBox8.Checked = true;

            break;
        case Keys.NumPad9:
            if (checkBox9.Checked)
                checkBox9.Checked = false;
            else
                checkBox9.Checked = true;

            break;
        default:
            break;
        }
    }

You can then create a common EventHandler for the CheckedChanged event and check for which Checkbox was selected to run the corresponding methods.

void checkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox cb = (CheckBox)sender;

    switch(cb.Name)
    {
        case "checkBox1":
            if (cb.Checked)
                // Method to use when checkBox1 is checked
            else
                // Method to use when checkBox1 is unchecked


            break;

        case "checkBox2":
            if (cb.Checked)
                // Method to use when checkBox2 is checked
            else
                // Method to use when checkBox2 is unchecked

            break;

        case "checkBox3":
            if (cb.Checked)
                // Method to use when checkBox3 is checked
            else
                // Method to use when checkBox3 is unchecked

            break;

        default:
            break;

        //Implement your other checkBox's the same way.
        }

    }
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • I've added this in, and put this.KeyPreview = true; into the Form2() (I also tried it in Form2_Keydown) and I still don't get the Form2_Keydown() to execute no matter which keys I press. – Tim Mar 22 '13 at 16:08
  • What form has focus when you are hitting your hot keys – Mark Hall Mar 22 '13 at 19:10
  • Also how are you adding your eventhandler to your Form2. it should be something like `this.KeyDown += new KeyEventHandler(Form2_KeyDown);` in your forms constructor – Mark Hall Mar 22 '13 at 23:09
  • public Form2() { InitializeComponent(); this.KeyPreview = true; this.KeyDown += new KeyEventHandler(Form2_KeyDown); } private void Form2_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.NumPad7: MessageBox.Show("ABC"); break; default: break; } } Sorry, I'm not sure where else to put the code. I'm not sure which window has foc – Tim Mar 24 '13 at 22:59
  • The code I gave you **is** working code, **only** if the Form it is placed in has focus. If you want to do this Globally you will need to create a Global Hook. – Mark Hall Mar 25 '13 at 02:49
  • It's working now. I found the KeyPreview property in the properties window, and once I set that it worked. I have a related issue however. I"m not sure if I should post a new topic or not. Now it works, but I'm trying to use it on a form with tabs. The tabs each contain a set of 9 checkboxes that I'd like to have this work on, but I'm not sure how to localize the switch statement to each tab. – Tim Mar 27 '13 at 21:57
1

Now it's possible to simply add an ampersand & before the letter you want to be enabled with ALT. For more info:

https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-create-access-keys?view=netdesktop-5.0

Leo
  • 67
  • 1
  • 10
0

Assign the same event handler to the events from all the check boxes:

_checkbox1.KeyDown += checkBox_KeyDown;
_checkbox2.KeyDown += checkBox_KeyDown;

// And so on...

Then, do the check inside it:

private void checkBox_KeyDown(object sender, KeyEventArgs e)
{
    var checkbox = sender as CheckBox;
    if (checkbox == null) { return; }

    if (e.KeyCode == Keys.NumPad1)
    {
        _checkbox1.IsChecked = !_checkbox1.IsChecked;
        return;
    }

    if (e.KeyCode == Keys.NumPad2)
    {
        _checkbox2.IsChecked = !_checkbox2.IsChecked;
        return;
    }

    // And so on...
}
maximpa
  • 1,958
  • 13
  • 16
  • The checkbox still needs to have the focus. The question specifies that it should work even if the checkbox doesn't have the focus. – Robert Harvey Mar 17 '13 at 23:18