.NET Framework:如何使RichTextBox成为只读?

时间:2009-07-19 12:34:49

标签: .net richtextbox readonly

将RichTextBox设置为“ReadOnly”不会阻止通过双击编辑嵌入对象(如方程式)。我可以禁用控件,但后面有一个灰色的背景(不能只用BackColor更改),无法滚动。我试图在派生类中重写OnDoubleClick但没有成功。

4 个答案:

答案 0 :(得分:3)

我找到了解决方案! :)在派生类中:

protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0203) // WM_LBUTTONDBLCLK
        {
            // Do nothing
        }
        else
        {
            base.WndProc(ref m);
        }
    }

答案 1 :(得分:2)

我遇到了类似的问题,Entrase的答案是一个好的开始。不幸的是,控件仍然允许选择文本并删除它。我最终使用了以下代码:

Public Class ReadOnlyRichTextBox : Inherits Windows.Forms.RichTextBox

    Protected mOkayKeys As Windows.Forms.Keys() = {Windows.Forms.Keys.Up, Windows.Forms.Keys.Down, Windows.Forms.Keys.PageUp, Windows.Forms.Keys.PageDown}

    Private Sub ReadOnlyRichTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Control And (e.KeyCode = Windows.Forms.Keys.C) Then
            Exit Sub
        End If
        If Not mOkayKeys.Contains(e.KeyCode) Then e.Handled = True
    End Sub

    Private Sub ReadOnlyRichTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        e.Handled = True
    End Sub

    Private Sub ReadOnlyRichTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.Control And (e.KeyCode = Windows.Forms.Keys.C) Then
            Exit Sub
        End If
        If Not mOkayKeys.Contains(e.KeyCode) Then e.Handled = True
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Windows.Forms.Message)
        If (m.Msg = &H203) Then ' WM_LBUTTONDBLCLK=0x0203
            ' Do nothing
        Else
            MyBase.WndProc(m)
        End If
    End Sub

End Class

答案 2 :(得分:0)

嗯......只需在双击时尝试将Sellength设置为0。是不是有一个只读/锁定的财产?

答案 3 :(得分:0)

这可以按照以下方式完成

1)将RichTextBox属性ReadOnly设置为true

2)转到属性 - >外观 - > BackColor 并将颜色设置为窗口