使用Visual Studio 2010在C#中编程热键 [英] Programming a Hotkey in C# with Visual Studio 2010

查看:64
本文介绍了使用Visual Studio 2010在C#中编程热键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小表格,里面有9个复选框。我正在尝试为与Numpad相对应的盒子制作热键,但时间最繁琐。我有两个主要问题:

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");

        }
    }

这是我的代码。它可以工作,但不能满足我的要求。它会显示一条消息,但仅当该复选框突出显示时才显示。我认为KeyPreview在这种情况下可能会有所帮助,但是MSDN数据库无法帮助我解决试图解决KeyPreview问题的问题。

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.

第二,我希望我按下热键时代码选中该框。我无法确定使用CheckState的组合似乎可行。如果有人煽动,我将不胜感激。

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.

注释中的代码:

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; 
    } 
} 


推荐答案

您的问题没有说明您是否希望它成为特定于应用程序的热键或系统热键。我假设它是特定于应用程序的。正确的假设是您需要将Forms KeyPreview属性设置为true。然后,您需要像下面这样在Forms Keydown事件中放置用于设置和清除复选框的代码:

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;
        }
    }

然后可以为CheckedChanged事件创建一个公共EventHandler并检查选择了哪个Checkbox来运行相应的方法。

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.
        }

    }

这篇关于使用Visual Studio 2010在C#中编程热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆