如何在System.Windows.Forms.TextBox中隐藏输入插入符?

时间:2022-10-28 21:10:08

I need to display a variable-length message and allow the text to be selectable. I have made the TextBox ReadOnly which does not allow the text to be edited, but the input caret is still shown.

我需要显示一个可变长度的消息,并允许文本可选。我制作了TextBox ReadOnly,它不允许编辑文本,但仍显示输入插入符号。

The blinking input caret is confusing. How do I hide it?

闪烁的输入插入符号令人困惑。我怎么隐藏它?

5 个解决方案

#1


12  

You can do through a win32 call

你可以通过win32通话

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public void HideCaret()
{
    HideCaret(someTextBox.Handle);
}

#2


5  

When using the win32 call don't forget to hide the cursor in the textbox's GotFocus event.

使用win32时,不要忘记将光标隐藏在文本框的GotFocus事件中。

#3


3  

Just for completeness, I needed such a functionality for using with a DevExpress WinForms TextEdit control.

为了完整起见,我需要这样的功能来使用DevExpress WinForms TextEdit控件。

They already do provide a ShowCaret and a HideCaret method, unfortunately they are protected. Therefore I created a derived class that provides the functionality. Here is the full code:

他们已经提供了ShowCaret和HideCaret方法,不幸的是它们受到保护。因此,我创建了一个提供功能的派生类。这是完整的代码:

public class MyTextEdit : TextEdit
{
    private bool _wantHideCaret;

    public void DoHideCaret()
    {
        HideCaret();

        _wantHideCaret = true;
    }

    public void DoShowCaret()
    {
        ShowCaret();

        _wantHideCaret = false;
    }

    protected override void OnGotFocus(EventArgs e)
    {
        base.OnGotFocus(e);

        if (_wantHideCaret)
        {
            HideCaret();
        }
    }
}

To use the code, simply use the derived class instead of the original TextEdit class in your code and call DoHideCaret() anywhere, e.g. in the constructor of your form that contains the text edit control.

要使用代码,只需在代码中使用派生类而不是原始TextEdit类,并在任何地方调用DoHideCaret(),例如在包含文本编辑控件的窗体的构造函数中。

Maybe this is helpful to someone in the future.

也许这对将来的某个人有帮助。

#4


1  

If you disable the text box (set Enable=false), the text in it is still scrollable and selectable. If you don't like the visual presentation of a disabled text box (gray background usually) you can manually override the colors.

如果禁用文本框(设置Enable = false),则其中的文本仍可滚动并可选。如果您不喜欢禁用文本框的视觉呈现(通常是灰色背景),您可以手动覆盖颜色。

Be warned, manually overriding colors is going to make your form/control look weird on systems that do not use the default color/theme settings. Don't assume that because your control is white that everyone's control is going to be white. That's why you should always use the system colors whenever possible (defined in the System.Drawing.SystemColors enumeration) such as SystemColors.ControlLight.

请注意,手动覆盖颜色会使您的表单/控件在不使用默认颜色/主题设置的系统上看起来很奇怪。不要以为因为你的控制是白色的,所以每个人的控制都是白色的。这就是为什么你应该尽可能使用系统颜色(在System.Drawing.SystemColors枚举中定义),例如SystemColors.ControlLight。

#5


-4  

AFAIK, this cannot be done. The TextBox control is a funny control because it actually has a lot of behaviour that can't be modified due to the way it taps into the operating system. This is why many of the cool custom TextBoxes are written from scratch.

AFAIK,这是不可能做到的。 TextBox控件是一个有趣的控件,因为它实际上有很多行为,由于它进入操作系统的方式无法修改。这就是为什么许多很酷的自定义TextBox都是从头开始编写的。

I am afraid you may not be able to do what you wish to do :(

我担心你可能无法做你想做的事:(

#1


12  

You can do through a win32 call

你可以通过win32通话

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public void HideCaret()
{
    HideCaret(someTextBox.Handle);
}

#2


5  

When using the win32 call don't forget to hide the cursor in the textbox's GotFocus event.

使用win32时,不要忘记将光标隐藏在文本框的GotFocus事件中。

#3


3  

Just for completeness, I needed such a functionality for using with a DevExpress WinForms TextEdit control.

为了完整起见,我需要这样的功能来使用DevExpress WinForms TextEdit控件。

They already do provide a ShowCaret and a HideCaret method, unfortunately they are protected. Therefore I created a derived class that provides the functionality. Here is the full code:

他们已经提供了ShowCaret和HideCaret方法,不幸的是它们受到保护。因此,我创建了一个提供功能的派生类。这是完整的代码:

public class MyTextEdit : TextEdit
{
    private bool _wantHideCaret;

    public void DoHideCaret()
    {
        HideCaret();

        _wantHideCaret = true;
    }

    public void DoShowCaret()
    {
        ShowCaret();

        _wantHideCaret = false;
    }

    protected override void OnGotFocus(EventArgs e)
    {
        base.OnGotFocus(e);

        if (_wantHideCaret)
        {
            HideCaret();
        }
    }
}

To use the code, simply use the derived class instead of the original TextEdit class in your code and call DoHideCaret() anywhere, e.g. in the constructor of your form that contains the text edit control.

要使用代码,只需在代码中使用派生类而不是原始TextEdit类,并在任何地方调用DoHideCaret(),例如在包含文本编辑控件的窗体的构造函数中。

Maybe this is helpful to someone in the future.

也许这对将来的某个人有帮助。

#4


1  

If you disable the text box (set Enable=false), the text in it is still scrollable and selectable. If you don't like the visual presentation of a disabled text box (gray background usually) you can manually override the colors.

如果禁用文本框(设置Enable = false),则其中的文本仍可滚动并可选。如果您不喜欢禁用文本框的视觉呈现(通常是灰色背景),您可以手动覆盖颜色。

Be warned, manually overriding colors is going to make your form/control look weird on systems that do not use the default color/theme settings. Don't assume that because your control is white that everyone's control is going to be white. That's why you should always use the system colors whenever possible (defined in the System.Drawing.SystemColors enumeration) such as SystemColors.ControlLight.

请注意,手动覆盖颜色会使您的表单/控件在不使用默认颜色/主题设置的系统上看起来很奇怪。不要以为因为你的控制是白色的,所以每个人的控制都是白色的。这就是为什么你应该尽可能使用系统颜色(在System.Drawing.SystemColors枚举中定义),例如SystemColors.ControlLight。

#5


-4  

AFAIK, this cannot be done. The TextBox control is a funny control because it actually has a lot of behaviour that can't be modified due to the way it taps into the operating system. This is why many of the cool custom TextBoxes are written from scratch.

AFAIK,这是不可能做到的。 TextBox控件是一个有趣的控件,因为它实际上有很多行为,由于它进入操作系统的方式无法修改。这就是为什么许多很酷的自定义TextBox都是从头开始编写的。

I am afraid you may not be able to do what you wish to do :(

我担心你可能无法做你想做的事:(