How to disable cursor in textbox?

Viewed 76797

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?

11 Answers

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

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;

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.

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.

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
Related