在代码后端更改文本框背景色

14

我该如何在代码后端将文本框的背景颜色更改为以下形式:

Textbox1.BackColor = "#F2F0E1
代替
Textbox1.BackColor = System.Drawing.White
3个回答

49

你可以尝试类似以下这样的代码:

Textbox1.BackColor = System.Drawing.ColorTranslator.FromHtml("#F2F0E1");

完美运行,谢谢。 - Jithesh Chandra

3
.focusfld
{
    background-color: #FFFFCC;
}


   .normalfld
{
    background-color: #FFFFFF;
}

Javascript

function DoFocus(fld) 
{
    fld.className = 'focusfld';
}
function DoBlur(fld) 
{
    fld.className='normalfld';
}

代码后台

   TempTextBox.Attributes.Add("onFocus", "DoFocus(this);");
    TempTextBox.Attributes.Add("onBlur", "DoBlur(this);"); 

0
创建一个函数从十六进制字符串获取颜色。
    public Color HexColor(String hex)
    {
     //remove the # at the front
     hex = hex.Replace("#", "");

     byte a = 255;
     byte r = 255;
     byte g = 255;
     byte b = 255;

     int start = 0;

     //handle ARGB strings (8 characters long)
     if (hex.Length == 8)
     {
         a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
         start = 2;
     }

     //convert RGB characters to bytes
     r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
     g = byte.Parse(hex.Substring(start+2, 2), System.Globalization.NumberStyles.HexNumber);
     b = byte.Parse(hex.Substring(start+4, 2), System.Globalization.NumberStyles.HexNumber);

     return Color.FromArgb(a, r, g, b);
    }

然后设置

 Color c = HexColor("#F2F0E1");

Textbox1.BackColor =  HexColor("#F2F0E1");

或者

Textbox1.BackColor = c;

参考资料:http://silverlemma.blogspot.com/2009/03/c-converting-from-hex-string-to-color.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接