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

时间:2022-03-31 20:06:59

Setting RichTextBox as “ReadOnly” doesn't prevent embedded objects (like equations) from being edited by double-clicking them. I could disable the control but then there is a gray background (can't be just changed with BackColor) and no way to scroll. I tried to override OnDoubleClick in a derived class but no success.

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

4 个解决方案

#1


I found a solution! :) In a derived class:

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

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

#2


I had a similar problem and the answer from Entrase was a good start. Unfortunately, the control still allowed selecting text and deleting it. I ended up using the following code:

我有一个类似的问题,来自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

#3


Hmm... Just try setting Sellength to 0 on doubleclick. Isn't there a readonly/locked property?

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

#4


This can be done as follows

这可以如下完成

1)Set the RichTextBox property ReadOnly to true

1)将RichTextBox属性ReadOnly设置为true

2)Go to Properties->Appearance->BackColor and set color as Window

2)转到Properties-> Appearance-> BackColor并将颜色设置为Window

#1


I found a solution! :) In a derived class:

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

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

#2


I had a similar problem and the answer from Entrase was a good start. Unfortunately, the control still allowed selecting text and deleting it. I ended up using the following code:

我有一个类似的问题,来自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

#3


Hmm... Just try setting Sellength to 0 on doubleclick. Isn't there a readonly/locked property?

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

#4


This can be done as follows

这可以如下完成

1)Set the RichTextBox property ReadOnly to true

1)将RichTextBox属性ReadOnly设置为true

2)Go to Properties->Appearance->BackColor and set color as Window

2)转到Properties-> Appearance-> BackColor并将颜色设置为Window