29

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?

Phillip
  • 255
  • 1
  • 12
Matjas
  • 291
  • 1
  • 3
  • 3

11 Answers11

28

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);
    }
}
Mikhail Semenov
  • 953
  • 8
  • 8
16

In C# you can disable the cursor in a textbox by temporarily disabling and then re-enabling the text box whenever it receives the focus. Note there is no need to make the textbox read only if using this method. For example:

private void TextBox_Enter(object sender, EventArgs e)
{
  TextBox.Enabled = false;
  TextBox.Enabled = true;
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Paul Jerome
  • 161
  • 1
  • 2
15

You could use a Label instead. When in the designer, you set BorderStyle = Fixed3D, BackColor = Window and AutoSize = False, it looks a lot like a TextBox.

However, the cursor in a TextBox is provided so that the user can scroll through the text when it is longer than the box. You'll lose that functionality with a Label, unless you are sure that it will always fit. Other than that, it is not possible to remove the cursor from a TextBox.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • A ReadOnly textbox is also useful for displaying information that the user might want to copy and paste somewhere else. By using a Label (or otherwise disabling the cursor) that ability is lost or reduced. But if you truly want to display information without the ability to select it, then a Label is the right tool for the job. – Jeffrey L Whitledge Sep 16 '10 at 21:42
  • @JeffreyLWhitledge I know this is an old question but just to weight in, what happens if the user requires scrollbars on a label? – Master Yoda Mar 03 '15 at 16:57
  • @KyleT: If you have a sufficiently big amount of text that you require scroll bars, then I suggest you still use a `TextBox` instead, and set it's `ReadOnly` property to `true`. The cursor in there is actually nice to have, since it allows the user to select parts of the text without editing it. – Daniel A.A. Pelsmaeker Mar 03 '15 at 19:30
9

Putting the hideCaret function inside the TextChanged event will solve the problem:

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

private void textBox1_TextChanged(object sender, EventArgs e)
{
    HideCaret(textBox1.Handle);
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
kral06
  • 119
  • 1
  • 1
7

Easiest solution for me was to just override the on focus event and focus back to the parent. This prevents the cursor and any editing of the textbox by the user and basically disables the text box with out having to set the Enabled = false property.

private void Form1_load(object sender, EventArgs e) {
    textBox1.ReadOnly = true;
    textBox1.Cursor = Cursors.Arrow;
    textBox1.GotFocus += textBox1_GotFocus;
}


private void textBox1_GotFocus(object sender, EventArgs e) {
    ((TextBox)sender).Parent.Focus();
}
Troublesum
  • 257
  • 4
  • 10
5

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;
Kevin Shieh
  • 51
  • 1
  • 2
2

You can set it programatically.

textBox1.Cursor = Cursors.Arrow;
Gjaa
  • 1,461
  • 1
  • 19
  • 20
0

This is not strictly an answer to the question, but perhaps it can solve some similar problem(s). I use a textbox control which can look like a label for a control which displays a scale, but can be edited, when clicked. Start enabled = false and make an activation (enabled = true) in a mousehandler of the parent of the textbox control (which, when disabled, border None and backcolor = parent backcolor, looks like a label). E.g. when enter hit or other event, disable again in KeyDown handler. (Of course the parent mouse click routine can check whether the mouseclick really occured in the label/textbox control). If you need the textbox control to activate by tabbing, some more work is required (than I have done). I use the form constructor to find the textbox parent at runtime and to apply the delegate mouse control. Perhaps you can do this as wel in compile time (Form header), but that seemed a little error-prone to me.

Jan
  • 447
  • 6
  • 8
0

One way of doing it is using View + TabIndex, you can do indexing of some other controls on the dialog as first, let say for buttons if there any. Then as if the control tabIndex is not the first i.e 0, cursor won't get appear there.

0

To disable the edit and cursor in the TEXT BOX

this.textBox.ReadOnly = true; 
this.textBox.Cursor = Cursors.No;//To show a red cross icon on hover
this.textBox.Cursor = Cursors.Arrow //To disable the cursor
jithu
  • 1,867
  • 1
  • 7
  • 16
-6

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.

yogendra
  • 341
  • 2
  • 4
  • 11
  • 4
    Only makes things worse. Also changes alignment, the cursor still appears where you click and selecting text is... strange. – Gert Arnold Aug 09 '12 at 19:32