.NET Framework:如何使RichTextBox真正只读?

发布于 2024-07-28 23:27:12 字数 139 浏览 3 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贱贱哒 2024-08-04 23:27:12

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

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

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);
        }
    }
檐上三寸雪 2024-08-04 23:27:12

我遇到了类似的问题,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

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:

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
桃酥萝莉 2024-08-04 23:27:12

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

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

坐在坟头思考人生 2024-08-04 23:27:12

可以按如下方式完成

1) 将 RichTextBox 属性 ReadOnly 设置为 true

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

This can be done as follows

1)Set the RichTextBox property ReadOnly to true

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文