Override ShortCut keys on .NET RichTextBox

I use RichTextBox (.NET WinForms 3.5) and would like to override some standard ShortCut keys .... For example, I don’t want Ctrl + I to make italics using the RichText method, but instead run my own text processing method.

Any ideas?

+1
source share
2 answers

Ctrl + I is not one of the default shortcuts affecting the ShortcutsEnabled property.

The following code intercepts Ctrl + I in the KeyDown event, so you can do whatever you want inside the if block, just be sure to suppress the keystroke as 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;
    }
}
+4
source

RichtTextBox.ShortcutsEnabled true, , KeyUp. .

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

All Articles