【问题标题】:Change textbox BackColor in code-behind在代码隐藏中更改文本框 BackColor
【发布时间】:2010-10-06 06:50:32
【问题描述】:

如何将代码隐藏中的文本框 BackColor 更改为如下内容:

Textbox1.BackColor = "#F2F0E1

而不是

Textbox1.BackColor = System.Drawing.White

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    你可以试试这样的:

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

    【讨论】:

    • 完美运行谢谢。
    【解决方案2】:

    样式表

    .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);"); 
    

    【讨论】:

      【解决方案3】:

      创建一个函数从十六进制字符串中获取颜色


          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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-23
        • 2015-08-10
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 1970-01-01
        • 2015-10-21
        相关资源
        最近更新 更多