Override ShortCut Keys on .NET RichTextBox

我只是一个虾纸丫 提交于 2019-12-02 03:21:36

Ctrl + I isn't one of the default shortcuts affected by the ShortcutsEnabled property.

The following code intercepts the Ctrl + I in the KeyDown event so you can do anything you want inside the if block, just make sure to suppress the key press like I've shown.

private void YourRichTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.I)
    {
        // do whatever you want to do here...
        e.SuppressKeyPress = true;
    }
}

Set the RichtTextBox.ShortcutsEnabled property to true and then handle the shortcuts yourself, using the KeyUp event. E.G.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.ShortcutsEnabled = false;
            this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
        }

        void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.KeyCode == Keys.X)
                MessageBox.Show("Overriding ctrl+x");
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!