开发者

Assign foreground color property to a string dynamically

I have a label and i assigned foreground color to it dynamically using the following code:

string xCol = "#F8951D";
Color c = System.Drawing.ColorTranslator.FromHtml(xCol);
string abc = Convert.ToString(c);
string[] col = abc.Split();
for (int k = 1; k <= 4; k++)
{
    col[k] = col[k].Replace("A", "");
    col[k] = col[k].Replace("=", "");
    col[k] = col[k].Replace("[", "");
    col[k] = col[k].Replace(",", "");
    col[k]开发者_StackOverflow = col[k].Replace("R", "");
    col[k] = col[k].Replace("G", "");
    col[k] = col[k].Replace("B", "");
    col[k] = col[k].Replace("]", "");
    if (k == 1)
    { int abc1 = Convert.ToInt32(col[k]); Session["abc1"] = abc1; }
    else if (k == 2)
    { int abc2 = Convert.ToInt32(col[k]); Session["abc2"] = abc2; }
    else if (k == 3)
    { int abc3 = Convert.ToInt32(col[k]); Session["abc3"] = abc3; }
    else if (k == 4)
    { int abc4 = Convert.ToInt32(col[k]); Session["abc4"] = abc4; }
}

int c1 = Convert.ToInt32(Session["abc1"].ToString());
int c2 = Convert.ToInt32(Session["abc2"].ToString());
int c3 = Convert.ToInt32(Session["abc3"].ToString());
int c4 = Convert.ToInt32(Session["abc4"].ToString());
lblDept.ForeColor = Color.FromArgb(c1,c2,c3,c4);

How can I assign a foreground color property to a string dynamically using a,r,g,b components?

Is it possible to assign it using string strColor = Color.FromArgb(c1,c2,c3,c4);?

How can I set a color property to a string dynamically?


there is no way to assign a color to a string, a .NET string is a data type which contains no formatting but only a collection on chars that can be used to render some text. Your question is like assigning a font to an integer, an integer int only contains digits, like 1 or 150, no other attributes.

Said so, if in the user interface of your application (Windows Based or Web Based) you want to render some text using a specific Font ( so character family, like Arial, size, and color ), you should use a UI control like a label or a TextBox which usually allows you to set the text content in a .Text property and allows you to also manipulate the style by using other properties. But such UI control is not a string, is a container of some logic and graphics which serves to render strings with a specific formatting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜