C# 使文本框不可编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14598024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:23:25  来源:igfitidea点击:

Make TextBox uneditable

c#.netwinformstextbox

提问by Mahdi Tahsildari

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

我想让TextBox我的表单上的一些es 不可编辑,但我希望它text是清晰的(黑色而不是灰色),这就是我不想使用的原因

myTextBox.Enabled = false;

myTextBox.Enabled = false;

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

不知何故,我希望它被禁用,但前色为非灰色。

Does anyone have any clue?

有没有人有任何线索?

采纳答案by Parimal Raj

Using the TextBox.ReadOnlyproperty

使用TextBox.ReadOnly物业

TextBox.ReadOnly = true;

For a Non-Grey background you can change the TextBox.BackColorproperty to SystemColors.WindowColor

对于非灰色背景,您可以将TextBox.BackColor属性更改为SystemColors.Window颜色

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.

当此属性设置为 true 时,用户在运行时无法更改控件的内容。将此属性设置为 true 后,您仍然可以在代码中设置 Text 属性的值。您可以使用此功能而不是禁用具有 Enabled 属性的控件,以允许复制内容和显示工具提示。

回答by Habib

Use the ReadOnlyproperty on the TextBox.

在 TextBox 上使用ReadOnly属性。

myTextBox.ReadOnly = true;

But Remember: TextBoxBase.ReadOnly Property

但请记住:TextBoxBase.ReadOnly 属性

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.

当此属性设置为 true 时,用户在运行时无法更改控件的内容 。将此属性设置为 true 后,您仍然可以在代码中设置 Text 属性的值。您可以使用此功能而不是禁用具有 Enabled 属性的控件,以允许复制内容和显示工具提示。

回答by Marius Bancila

If you want your TextBoxuneditable you should make it ReadOnly.

如果你想要你的TextBox不可编辑,你应该让它成为ReadOnly

回答by Alina B.

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.

如果你想要一个非灰色的背景颜色,最后一行是必要的。

回答by A. Wolf

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

如果要使用 XAML 执行此操作,请将属性设置isReadOnlytrue.

回答by Jan Abramek

Just set in XAML:

只需在 XAML 中设置:

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

So that text will not be grayed-out.

这样文本就不会变灰。

回答by Dilip Kumar Choudhary

This is for GridView.

这是用于 GridView。

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