如何更改TextBox的背景颜色?

我得到的C#代码如下:

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

如何更改TextBox的背景颜色?

如果它是WPF,则静态类Brushes有一组颜色。

 TextBox.Background = Brushes.Red; 

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

 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; 

在WinForms和WebForms中,您可以:

 txtName.BackColor = Color.Aqua; 

web表单;

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

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

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

在css中使用backcolor属性指定它

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

注意: – 使用HTML和Javscript。

setInterval(function(){ab()},3000) ;” type =“button”value =“ClickMe”/>

var arr,i = 0; arr = [“Red”,“Blue”,“Green”,“Orange”,“Purple”,“Yellow”,“Brown”,“Lime”,“Gray”]; //我们提供数组作为输入。

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

注意:您可以使用setInterval 2nd参数更改毫秒数。

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

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

只有System.color的问题是它不包含某些基本颜色的定义,特别是白色,这是重要的原因,通常文本框是白色的;