How to disable cursor in textbox?

后端 未结 10 1103
执笔经年
执笔经年 2020-12-09 02:41

Is there any way to disable cursor in textbox without setting property Enable to false? I was trying to use ReadOnly property but despite the fact that I can\'t write in tex

10条回答
  •  盖世英雄少女心
    2020-12-09 02:57

    In C#, you can use the following read-only textbox:

    public class ReadOnlyTextBox : TextBox
    {
        [DllImport("user32.dll")]
        static extern bool HideCaret(IntPtr hWnd);
    
        public ReadOnlyTextBox()
        {
            this.ReadOnly = true;
            this.BackColor = Color.White;
            this.GotFocus += TextBoxGotFocus;
            this.Cursor = Cursors.Arrow; // mouse cursor like in other controls
        }
    
        private void TextBoxGotFocus(object sender, EventArgs args)
        {
            HideCaret(this.Handle);
        }
    }
    

提交回复
热议问题