问题
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;
}
}
回答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.
}
}
回答2:
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...
}
来源:https://stackoverflow.com/questions/15467306/programming-a-hotkey-in-c-sharp-with-visual-studio-2010