禁用RichTextBox WF上的删除按钮

时间:2013-06-07 20:07:07

标签: c# winforms

我正在尝试禁止人们删除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...
        }
    }

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我的解决方案:

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

旁注:是的,这会闪烁。仅限概念证明。要避免闪烁,请参阅How to append text to RichTextBox without scrolling and losing selection?

答案 2 :(得分:1)

而不是KeyPress事件在RichText Box中使用KeyDown。

尝试此操作以防止在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;
        }

答案 3 :(得分:0)

您必须添加Back键以防止删除:

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

答案 4 :(得分:0)

我的解决方案是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),它没有{{1这对于SelectionProtectedTextBox都可以使用(只需相应地更改以下代码中的类名):

RichTextBox