我正在使用RichTextBox(.NET WinForms 3.5),并希望覆盖一些标准的ShortCut键.... 例如,我不希望Ctrl + I通过RichText方法使文本斜体,而是运行我自己的方法来处理文本。

有什么想法吗?

有帮助吗?

解决方案

Ctrl + I不是受ShortcutsEnabled属性影响的默认快捷方式之一。

以下代码拦截了KeyDown事件中的Ctrl + I,因此您可以在if块中执行任何操作,只需确保按下我所显示的按键。

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

其他提示

将RichtTextBox.ShortcutsEnabled属性设置为true,然后使用KeyUp事件自行处理快捷方式。 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");
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top