How to hide the caret in a RichTextBox?

后端 未结 9 1699
[愿得一人]
[愿得一人] 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: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 
    {
        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;

提交回复
热议问题