Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

Make TextBox uneditable

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?

Upvotes: 77

Views: 185193

Answers (8)

Suraj Pandey
Suraj Pandey

Reputation: 1

Enabled="false" in aspx page

Upvotes: -1

Dilip Kumar Choudhary
Dilip Kumar Choudhary

Reputation: 469

This is for GridView.

 grid.Rows[0].Cells[1].ReadOnly = true;

Upvotes: 1

Jan Abramek
Jan Abramek

Reputation: 138

Just set in XAML:

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

So that text will not be grayed-out.

Upvotes: 4

A. Wolf
A. Wolf

Reputation: 1347

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

Upvotes: 1

Parimal Raj
Parimal Raj

Reputation: 20565

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.

Upvotes: 136

Alina B.
Alina B.

Reputation: 1276

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.

Upvotes: 6

Marius Bancila
Marius Bancila

Reputation: 16318

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

Upvotes: 3

Habib
Habib

Reputation: 223187

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.

Upvotes: 18

Related Questions