75

I want to make some TextBoxes on my form uneditable, but I want the text to be clear (black not gray) and that's why I do not want to use

myTextBox.Enabled = false;

Somehow I want it to be disabled but with non-gray fore-color.

Does anyone have any clue?

Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94

8 Answers8

132

Using the TextBox.ReadOnly property

TextBox.ReadOnly = true;

For a Non-Grey background you can change the TextBox.BackColor property to SystemColors.Window Color

textBox.BackColor = System.Drawing.SystemColors.Window;

When this property is set to true, the contents of the control cannot be changed by the user at runtime. With this property set to true, you can still set the value of the Text property in code. You can use this feature instead of disabling the control with the Enabled property to allow the contents to be copied and ToolTips to be shown.

jgawrych
  • 3,322
  • 1
  • 28
  • 38
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • 3
    The property is ```TextBox.IsReadOnly``` for WPF .NET 4.5.2, Visual Studio 2015, Update 3 (it says Version 14.0.25431.01 Update 3). – ecth May 31 '17 at 12:15
17

Use the ReadOnly property on the TextBox.

myTextBox.ReadOnly = true;

But Remember: TextBoxBase.ReadOnly Property

When this property is set to true, the contents of the control cannot be changed by the user at runtime. With this property set to true, you can still set the value of the Text property in code. You can use this feature instead of disabling the control with the Enabled property to allow the contents to be copied and ToolTips to be shown.

Habib
  • 219,104
  • 29
  • 407
  • 436
6

You can try using:

textBox.ReadOnly = true;
textBox.BackColor = System.Drawing.SystemColors.Window;

The last line is only neccessary if you want a non-grey background color.

Alina B.
  • 1,256
  • 8
  • 18
4

Just set in XAML:

        <TextBox IsReadOnly="True" Style="{x:Null}" />

So that text will not be grayed-out.

Jan Abramek
  • 138
  • 1
  • 5
3

If you want your TextBox uneditable you should make it ReadOnly.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
1

If you want to do it using XAML set the property isReadOnly to true.

A. Wolf
  • 1,309
  • 17
  • 39
1

This is for GridView.

 grid.Rows[0].Cells[1].ReadOnly = true;
4b0
  • 21,981
  • 30
  • 95
  • 142
-1

Enabled="false" in aspx page

  • 1
    Hi Suraj, welcome to Stackoverflow. Thanks for your effort. My question is actually about Windows Forms Applications. Tags help us put our questions in the proper category. – Mahdi Tahsildari Aug 25 '20 at 05:15