0

将 RichTextBox 设置为“ReadOnly”不会阻止通过双击嵌入的对象(如方程式)来对其进行编辑。我可以禁用该控件,但随后会出现灰色背景(不能仅使用 BackColor 更改)并且无法滚动。我试图在派生类中覆盖 OnDoubleClick 但没有成功。

4

4 回答 4

3

我找到了解决办法!:) 在派生类中:

protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0203) // WM_LBUTTONDBLCLK
        {
            // Do nothing
        }
        else
        {
            base.WndProc(ref m);
        }
    }
于 2009-07-19T12:58:35.447 回答
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
于 2012-05-10T19:17:56.353 回答
0

嗯...只需尝试在双击时将 Sellength 设置为 0。没有只读/锁定属性吗?

于 2009-07-19T12:38:35.033 回答
0

这可以按如下方式完成

1) 将 RichTextBox 属性 ReadOnly 设置为 true

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

于 2012-03-19T04:43:27.623 回答