C# 如何更改文本框的背景颜色?

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

How to change TextBox's Background color?

c#.netwpf

提问by SubZeroFX

I got C# code that is like:

我得到了类似的 C# 代码:

if(smth == "Open")
{
    TextBox.Background = ???
}    

How to change TextBox's background color?

如何更改 TextBox 的背景颜色?

采纳答案by Marius Bancila

If it's WPF, there is a collection of colors in the static class Brushes.

如果是WPF,静态类中有颜色的集合Brushes

TextBox.Background = Brushes.Red;

Of course, you can create your own brush if you want.

当然,您可以根据需要创建自己的画笔。

LinearGradientBrush myBrush = new LinearGradientBrush();
myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0));
TextBox.Background = myBrush;

回答by Dimitar Dimitrov

In WinForms and WebForms you can do:

在 WinForms 和 WebForms 中,您可以执行以下操作:

txtName.BackColor = Color.Aqua;

回答by Emre

webforms;

网络表格;

TextBox.Background = System.Drawing.Color.Red;

回答by Randhi Rupesh

in web application in .cs page

在 .cs 页面的 Web 应用程序中

   txtbox.Style.Add("background-color","black");

in css specify it by using backcolor property

在 css 中使用 backcolor 属性指定它

回答by Nav

It is txtName.BackColor = System.Drawing.Color.Red;

这是 txtName.BackColor = System.Drawing.Color.Red;

one can also use txtName.BackColor = Color.Aqua;which is the same as txtName.BackColor = System.Color.Aqua;

一个也可以使用txtName.BackColor = Color.Aqua;这是相同的txtName.BackColor = System.Color.Aqua;

Only Problem with System.color is that it does not contain a definition for some basic colors especially white, which is important cause usually textboxes are white;

System.color 的唯一问题是它没有包含一些基本颜色的定义,尤其是白色,这很重要,因为通常文本框是白色的;

回答by Geeking

Setting textbox backgroundcolor with multiple colors on single click.

单击即可设置多种颜色的文本框背景颜色。

Note:- using HTML and Javscript.

注意:- 使用 HTML 和 JavaScript。

< input id="ClickMe_btn" onclick="setInterval(function () { ab() }, 3000);" type="button" value="ClickMe" />

< input id="ClickMe_btn" onclick=" setInterval(function () { ab() }, 3000);" type="button" value="ClickMe" />

var arr, i = 0; arr = ["Red", "Blue", "Green", " Orange ", "Purple", "Yellow", "Brown", "Lime", "Grey"]; // We provide array as input.

var arr, i = 0; arr = [“红色”、“蓝色”、“绿色”、“橙色”、“紫色”、“黄色”、“棕色”、“石灰”、“灰色”]; // 我们提供数组作为输入。

    function ab()
     {   document.getElementById("Text").style.backgroundColor = arr[i];
          window.alert(arr[i]);
          i++;
     }

Note: You can change milliseconds, with setInterval 2nd parameter.

注意:您可以使用 setInterval 第二个参数更改毫秒。