Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override ShortCut Keys on .NET RichTextBox

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?

like image 391
Adam Haile Avatar asked Jan 22 '26 21:01

Adam Haile


1 Answers

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;
    }
}
like image 75
sliderhouserules Avatar answered Jan 24 '26 13:01

sliderhouserules