Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a textbox non-selectable using C#

I'm using C#/.NET for a Windows Forms application. I have a text box. How can I make the text box unselectable?

I don't want to disable the complete textbox.

like image 609
saeed Avatar asked May 10 '10 07:05

saeed


People also ask

How do I make a textBox not selectable?

Set textBox. Enabled = false to prevent selection (see here).

How to make textBox ReadOnly c#?

You can try using: textBox. ReadOnly = true; textBox.

How to set a text box to read only?

Set the TextBox control's ReadOnly property to true . With the property set to true , users can still scroll and highlight text in a text box without allowing changes.


2 Answers

In the 'Enter' event of the textbox set the ActiveControl to something else:

    private void txtMyTextbox_Enter(object sender, EventArgs e)
    {
        ActiveControl = objMyOtherControl;
    }
like image 105
Zesty Avatar answered Oct 11 '22 12:10

Zesty


You have a couple of options:

  1. Use a Label control instead.
  2. Set textBox.Enabled = false to prevent selection (see here).
like image 5
Chris Schmich Avatar answered Oct 11 '22 13:10

Chris Schmich