禁用RichTextBox WF上的删除按钮

本文关键字:删除 按钮 RichTextBox WF 禁用 | 更新日期: 2023-09-27 18:17:30

我试图禁止人们删除richtextbox中的文本框。该项目使用windows窗体。

下面是我的代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
    }

    void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)8)
        {
            e.Handled = true;
            MessageBox.Show("Try not to delete... write freely and openly");
            //The msgbox shows, but the delete still happens within the form.
        }
    }

不显示消息框,也不停止删除:

    private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.KeyDown += new KeyEventHandler(richTextBox1_KeyDown);
    }
    private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
        {
            e.Handled = true;
            MessageBox.Show("Delete Pressed");
            // Does not show message box...
        }
    }

禁用RichTextBox WF上的删除按钮

根据KeyPressEventArgs.KeyChar上的MSDN文档,您无法使用该事件获取或设置DELETE键。您将需要使用KeyEventArgs.KeyCode,订阅KeyDownKeyUp事件。

我的解决方案:

void richTextBox1_TextChanged(object sender, EventArgs e) {
  richTextBox1.SelectAll();
  richTextBox1.SelectionProtected = true;
  richTextBox1.Select(richTextBox1.Text.Length, 0);
}

边注:是的,这将闪烁。只是概念验证。要避免闪烁,请参阅如何将文本追加到RichTextBox而不滚动和失去选择?

在RichText框中使用KeyDown代替KeyPress事件。

尝试这个来防止删除RichText Box

中的文本
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 46)
                e.Handled = true;
        }

如果你想禁止删除和退格,你可以改变KeyDown事件如下

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 8 || e.KeyValue == 46)
                e.Handled = true;
        }

必须添加返回键以防止删除:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
    {
        e.Handled = true;
        MessageBox.Show("Delete Pressed");
        // Does not show message box...
    }
}

编辑:

不可选择的RichTextBox:

public class ViewOnlyRichTextBox : System.Windows.Forms.RichTextBox {
    // constants for the message sending
    const int WM_SETFOCUS = 0x0007;
    const int WM_KILLFOCUS = 0x0008;
    protected override void WndProc(ref Message m) {
        if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
        base.WndProc (ref m);
    }
}

我的解决方案是结合SerkanOzvatan和LarsTech的答案。下面是代码:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
  {
    e.Handled = true;
    MessageBox.Show("Try not to delete... write freely and openly");
    // Does not show message box...
  }
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
   richTextBox1.SelectionProtected = richTextBox1.SelectionLength > 0;
}

效果很好:)

这是我自己的另一个解决方案,它也很有效,特别是如果你想做一个TextBox(不是RichTextBox),它没有SelectionProtected,这是用于TextBoxRichTextBox(只需改变类名在以下代码):

public class WritableRichTextBox : RichTextBox
{
    protected override bool ProcessKeyMessage(ref Message m)
    {
        int virtualKey = m.WParam.ToInt32();
        if (SelectionLength > 0 || virtualKey == 0x08 || virtualKey == 0x2e)
        {
            if (virtualKey != 0x25 && virtualKey != 0x26 && virtualKey != 0x27 && virtualKey != 0x28)
                return true;
        }
        return base.ProcessKeyMessage(ref m);
    }
}