Override ShortCut Keys on .NET RichTextBox

拥有回忆 提交于 2019-12-02 07:40:33

问题


I'm using a RichTextBox (.NET WinForms 3.5) and would like to override some of the standard ShortCut keys.... For example, I don't want Ctrl+I to make the text italic via the RichText method, but to instead run my own method for processing the text.

Any ideas?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/260716/override-shortcut-keys-on-net-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!