如何禁用文本框中的光标? [英] How to disable cursor in textbox?

查看:12
本文介绍了如何禁用文本框中的光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不将属性 Enable 设置为 false 的情况下禁用文本框中的光标?我试图使用 ReadOnly 属性,但尽管我无法在文本框中写入,但如果我单击文本框,则会出现光标.那么有什么办法可以永久去掉这个光标呢?

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# 中,您可以使用以下只读文本框:

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);
    }
}

这篇关于如何禁用文本框中的光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆