11

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.

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

Aidan Ryan
  • 11,389
  • 13
  • 54
  • 86
  • Does this answer your question? [How to disable cursor in textbox?](https://stackoverflow.com/questions/3730968/how-to-disable-cursor-in-textbox) – TylerH Apr 22 '21 at 14:23

6 Answers6

18

You can do through a win32 call

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public void HideCaret()
{
    HideCaret(someTextBox.Handle);
}
MEMPHIS
  • 29
  • 9
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • 2
    Dont forget `using System.Runtime.InteropServices;` and `ShowCaret(IntPtr hWnd);` – Sam Feb 13 '10 at 16:10
  • For whatever reason, I could not convince this to work on a RichTextBox - but when switching to a standard TextBox, it worked great (provided that it gets called in each `GotFocus` event, as per Landon's answer, and not just once) – chossenger May 17 '22 at 01:54
7

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

Landon
  • 71
  • 1
  • 1
3

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

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:

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.

Maybe this is helpful to someone in the future.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
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.

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.

Simon Gillbee
  • 3,932
  • 4
  • 35
  • 48
  • 4
    No, it's not selectable (or scrollable) if the text box is disabled. You are probably thinking of making it read-only. – Ian Goldby Feb 15 '17 at 12:53
-1

I know this is an old thread but it is a useful reference.

I solved the problem with a much easier but very kludgie solution, which may depend on how much control you have over the user's access to the form. I added a textbox (any focus-able control) which I gave prime tabIndex value and then positioned it off-form so that it was not visible. This works fine on a dialog because the user can't resize. If the form is resizeable, this may not work.

As I said, a kludge - but a lot easier to set up. (BTW I found the HideCaret approach didn't work - but I didn't pursue it hard.)

  • This doesn't sound useful for a situation where the input field needs to be visible, which I would expect would be pretty much any situation. If the field itself isn't going to be visible, then you shouldn't need to hide the caret separately. – TylerH Apr 22 '21 at 14:22
-3

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.

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

Rob Cooper
  • 28,567
  • 26
  • 103
  • 142