【问题标题】:.NET Framework: How to make RichTextBox true read-only?.NET Framework:如何使 RichTextBox 成为真正的只读?
【发布时间】:2009-07-19 12:34:49
【问题描述】:

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

【问题讨论】:

    标签: .net richtextbox readonly


    【解决方案1】:

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

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

    【讨论】:

      【解决方案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
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 没有。它将在不提及选择的情况下处理双击。正如我所说,“只读”属性没有帮助。
        【解决方案4】:

        可以按如下方式完成

        1)将 RichTextBox 属性 ReadOnly 设置为 true

        2)进入Properties->Appearance->BackColor,设置颜色为Window

        【讨论】:

        • 或者只是将locked属性设置为true
        猜你喜欢
        • 2010-12-14
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多