How to hide the caret in a RichTextBox?

后端 未结 9 1504
[愿得一人]
[愿得一人] 2020-12-31 15:10

Just like the title: I\'ve searched the web for an answer, but i was not able to find a way to hide the caret of a RichTextBox in VB.NET.

I\'ve tried to set the Rich

相关标签:
9条回答
  • 2020-12-31 15:33

    Do something to keep it from getting the 'input focus': it will have the caret, and be editable, only while it has the focus.

    0 讨论(0)
  • 2020-12-31 15:36

    Solution:

    from: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21896403.html

    using System;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    
    public class ReadOnlyRichTextBox : System.Windows.Forms.RichTextBox
    {
    
      [DllImport("user32.dll")]
      private static extern int HideCaret (IntPtr hwnd);
    
      public ReadOnlyRichTextBox()
      {
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
        this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ReadOnlyRichTextBox_Mouse);
        base.ReadOnly = true;
        base.TabStop = false;
        HideCaret(this.Handle);
      }
    
    
      protected override void OnGotFocus(EventArgs e)
      {
        HideCaret(this.Handle);
      }
    
      protected override void OnEnter(EventArgs e)
      {
        HideCaret(this.Handle);
      }
    
      [DefaultValue(true)]
      public new bool ReadOnly
      {
        get { return true; }
        set { }
      }
    
      [DefaultValue(false)]
      public new bool TabStop
      {
        get { return false; }
        set { }
      }
    
      private void ReadOnlyRichTextBox_Mouse(object sender, System.Windows.Forms.MouseEventArgs e)
      {
        HideCaret(this.Handle);
      }
    
      private void InitializeComponent()
      {
        //
        // ReadOnlyRichTextBox
        //
        this.Resize += new System.EventHandler(this.ReadOnlyRichTextBox_Resize);
    
      }
    
      private void ReadOnlyRichTextBox_Resize(object sender, System.EventArgs e)
      {
        HideCaret(this.Handle);
    
      }
    }
    
    0 讨论(0)
  • 2020-12-31 15:36

    Place the richTextBox control on the form

    Set form name as Form1

    Set richTextBox name as richTextBox1

    If you do not want to allow user to copy the text set richTextBox1 ShortcutsEnabled property to False

    Go to Project->Add Component, enter name of the component ReadOnlyRichTextBox.cs

    Then open ReadOnlyRichTextBox.cs and paste following code:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace <Replace with your app namespace>
    {
        public partial class ReadOnlyRichTextBox : RichTextBox
        {
            [DllImport("user32.dll")]
            static extern bool HideCaret(IntPtr hWnd);
    
            public ReadOnlyRichTextBox()
            {
                this.ReadOnly = true;
            }
    
            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                HideCaret(this.Handle);
            }
        }
    }
    

    From Solution Explorer open your "Form1.Designer.cs" and replace in this file the following lines:

    this.richTextBox1 = new System.Windows.Forms.RichTextBox();

    with

    this.richTextBox1 = new ReadOnlyRichTextBox();

    and

    private System.Windows.Forms.RichTextBox richTextBox1;

    with

    private ReadOnlyRichTextBox richTextBox1;

    0 讨论(0)
  • 2020-12-31 15:40

    You can use the HideCaret API function, Check it out on www.pinvoke.net. The trick is to know when to call it. A very simple and dirty solution is to start a one-shot timer in the RTF's Enter event. Trapping the correct message in the WndProc as nobugs suggestes is better, unfortunately the message trapped is wrong...

    0 讨论(0)
  • 2020-12-31 15:46

    I know it's old, but i see a lot of different options here. I must add that for me, this did the trick :

    this.textRichBox.ReadOnly = false;
    this.textRichBox.TabStop = false;
    
    0 讨论(0)
  • 2020-12-31 15:46

    Here I have a Rich Text control named txtMessage, it's events are handled to hide caret on events that would show it.

    <System.Runtime.InteropServices.DllImport("user32.dll")>
    Private Shared Function HideCaret(ByVal hWnd As IntPtr) As Boolean
    End Function
    
    Public Sub New()
        txtMessage.ReadOnly = True
        txtMessage.TabStop = False
    End Sub
    
    Private Sub txtMessage_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMessage.KeyUp
        HideCaret(txtMessage.Handle)
    End Sub
    
    Private Sub txtMessage_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtMessage.MouseDown
        HideCaret(txtMessage.Handle)
    End Sub
    
    Private Sub txtMessage_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtMessage.SelectionChanged
        HideCaret(txtMessage.Handle)
    End Sub
    
    0 讨论(0)
提交回复
热议问题