代码之家  ›  专栏  ›  技术社区  ›  Shekhar

如何在windows窗体中为文本框指定快捷键(如ctrl+f)?

  •  8
  • Shekhar  · 技术社区  · 16 年前

    我正在用c构建一个工具。这是一个windows应用程序。我在表单上有一个文本框,当用户按 Ctrl键 + f Ctrl键 + S .

    我该怎么做?

    4 回复  |  直到 7 年前
        1
  •  2
  •   Wayne    9 年前

    捕获 KeyDown 事件并在其中放置if语句,以检查按了哪些键。

    private void form_KeyDown(object sender, KeyEventArgs e)
    {
        if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S)) {
            txtSearch.Focus();
        }
    }
    
        2
  •  12
  •   Peter Mortensen Pieter Jan Bonestroo    14 年前

    一种方法是重写processCmdKey事件。

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.S))
        {
            MessageBox.Show("Do Something");
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
    

    编辑:也可以使用keydown事件-请参见 How to capture shortcut keys in Visual Studio .NET .

        3
  •  0
  •   CeejeeB    16 年前

    添加一个捕获窗体上按键的事件,分析按键并查看它是否与您的快捷键之一匹配,然后指定焦点。

        4
  •  0
  •   Mangesh Chaurasia    7 年前

    首先要确保windows窗体属性是“keypreview=true”

    第二件事打开表单事件属性并双击“keydown” 和 在事件主体内编写以下代码:

    private void form1_KeyDown(object sender, KeyEventArgs e)
    {
         if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode ==Keys.S)) 
         {
               TextBox1.Focus();
         }
    }
    
    推荐文章