Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide the input caret in a System.Windows.Forms.TextBox?

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?

like image 519
Aidan Ryan Avatar asked Sep 04 '08 16:09

Aidan Ryan


People also ask

How do I get rid of the flashing cursor in my TextBox?

You could set a maxlength on the textbox and then use text-indent to move the cursor back more characters than the maxlength. For example, you could set maxlength=20 and then for the text box set text-indent: -20em that way the text starts out of the box's boundaries and can't ever come into view.

How to hide cursor in TextBox c#?

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.

How TextBox works in a Windows Form application?

WinForms TextBox controls are used to get inputs from the user and to display the inputs. TextBox control is generally used for editing text, but it can also be set to read-only. TextBoxes are used to display multiple lines of wrap text to the size of the control.

How do I insert a TextBox in Windows form?

Step 1: Create a windows form. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the TextBox control to set the Text property of the TextBox.


4 Answers

You can do through a win32 call

[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public void HideCaret()
{
    HideCaret(someTextBox.Handle);
}
like image 57
Lars Truijens Avatar answered Sep 23 '22 05:09

Lars Truijens


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

like image 36
Landon Avatar answered Sep 23 '22 05:09

Landon


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.

like image 24
Uwe Keim Avatar answered Sep 23 '22 05:09

Uwe Keim


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.

like image 24
Simon Gillbee Avatar answered Sep 19 '22 05:09

Simon Gillbee