How to disable cursor in textbox?

2020-02-03 10:19发布

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 textbox, the cursor appears if I click the textbox. So is there any way to get rid of this cursor permamently?

标签: c# textbox
10条回答
贪生不怕死
2楼-- · 2020-02-03 11:04

you can use RightToLeft Property of Text Box, set it to true, you will not get rid of the Cursor, but it will get fixed at right corner and it will not appear automatically after every text you type in your text Box. I have used this to develop an application like Windows Calculator.

查看更多
Explosion°爆炸
3楼-- · 2020-02-03 11:05

Like @Mikhail Semenov 's solution, you can also use lambda express to quickly disable the cursor if you do not have many textboxes should do that:

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);

textBox1.ReadOnly = true;
textBox1.BackColor = Color.White;
textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
textBox1.Cursor = Cursors.Arrow;
查看更多
Juvenile、少年°
4楼-- · 2020-02-03 11:07

You can set it programatically.

textBox1.Cursor = Cursors.Arrow;
查看更多
我只想做你的唯一
5楼-- · 2020-02-03 11:08

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);
    }
}
查看更多
登录 后发表回答