2

我试图禁止人们删除富文本框中的文本框。该项目使用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...
        }
    }
4

5 回答 5

1

我的解决方案:

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

旁注:是的,这会闪烁。仅概念证明。要避免闪烁,请参阅如何在不滚动和丢失选择的情况下将文本附加到 RichTextBox?

于 2013-06-07T21:56:09.253 回答
1

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

试试这个以防止删除富文本框中的文本

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;
        }
于 2015-02-16T15:15:35.167 回答
1

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

于 2013-06-07T20:15:32.783 回答
0

My solution is some kind of the combination of SerkanOzvatan's and LarsTech's answers. Here is the code:

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

It works great :)

And here is another solution of my own which also works great, especially if you want to do with a TextBox (not a RichTextBox), it doesn't have a SelectionProtected, and this is used OK for both TextBox and RichTextBox (just change the class name in the following code accordingly):

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);
    }
}
于 2013-06-08T10:33:17.703 回答
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);
    }
}
于 2013-06-07T21:22:57.163 回答