Make TextBox uneditable

C#.NetWinformsTextbox

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

Just set in XAML:

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

So that text will not be grayed-out.

Solution 5 - C#

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

Solution 6 - C#

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

Solution 7 - C#

This is for GridView.

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

Solution 8 - C#

Enabled="false" in aspx page

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMahdi TahsildariView Question on Stackoverflow
Solution 1 - C#Parimal RajView Answer on Stackoverflow
Solution 2 - C#HabibView Answer on Stackoverflow
Solution 3 - C#Alina B.View Answer on Stackoverflow
Solution 4 - C#Jan AbramekView Answer on Stackoverflow
Solution 5 - C#Marius BancilaView Answer on Stackoverflow
Solution 6 - C#A. WolfView Answer on Stackoverflow
Solution 7 - C#Dilip Kumar ChoudharyView Answer on Stackoverflow
Solution 8 - C#Suraj PandeyView Answer on Stackoverflow