如何在 C# 中使 TextBox 非交互式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/874716/
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-05 05:01:40  来源:igfitidea点击:

How to make a TextBox non-interactive in C#

c#textbox

提问by

I've got a TextBox on a form that I am using to just display data. I have set it to ReadOnly which stops the user from changing the data displayed in the TextBox. Although the user can still highlight the data in the TextBox, how do I disable this?

我在用于显示数据的表单上有一个 TextBox。我已将其设置为 ReadOnly,这会阻止用户更改 TextBox 中显示的数据。尽管用户仍然可以突出显示 TextBox 中的数据,但如何禁用它?

NOTE: I know using a label would be much easier, although for some reason the label wont let me display the data how I would like. I want to display a number starting from the right of the display area, IE: " 25", although the label displays it like "25 ". I have tried making the label right-justified this does not solve the issue. Therefore I am using a TextBox.

注意:我知道使用标签会容易得多,尽管出于某种原因,标签不会让我以我想要的方式显示数据。我想从显示区域的右侧开始显示一个数字,即:“25”,尽管标签显示它像“25”。我曾尝试使标签正确对齐,但这并不能解决问题。因此我使用了一个文本框。

采纳答案by bbmud

You can display right aligned text in label by setting TextAlign property to e.g. MiddleRight. Remember also to set AutoSize property to false, otherwise it will resize the label and the position will change to left.

您可以通过将 TextAlign 属性设置为例如 MiddleRight 来在标签中显示右对齐文本。还要记住将 AutoSize 属性设置为 false,否则会调整标签大小并且位置会向左变化。

回答by

TextBox1.Enabled = false;

Or you can use the label with the property RightToLeft set to True

或者您可以使用属性 RightToLeft 设置为 True 的标签

回答by waqar khan

This is just a simple C#code and I just wrote it to disable some of my textboxes and button. But, you can change it as per your requirements:

这只是一个简单的C#代码,我只是编写它来禁用我的一些文本框和按钮。但是,您可以根据您的要求更改它:



TextBox[] tbArray = new TextBox[]
{
    custid,custname , field, email, phone, address
};
for (int i = 0; i < tbArray.Length; i++)
{
    if (status.SelectedIndex == 1)
    {
        tbArray[i].Enabled = false;
        savecust.Enabled = false;
        deletecust.Enabled = false;
    }
    else
    {
        tbArray[i].Enabled = true;
        savecust.Enabled = true;
        deletecust.Enabled = true;
    }
}   


回答by stulz

You can modify an event handler (say, on the Enter action) to move the Active property of the form to another control.

您可以修改事件处理程序(例如,在 Enter 操作上)以将表单的 Active 属性移动到另一个控件。