【问题标题】:Set color of text in a Textbox/Label to Red and make it bold将文本框/标签中的文本颜色设置为红色并使其变为粗体
【发布时间】:2012-04-16 22:59:16
【问题描述】:
我希望文本颜色在特定条件下为红色。
这就是我想要完成的方式。
string minusvalue = TextBox1.Text.ToString();
if (Convert.ToDouble(minusvalue) < 0)
{
// set color of text in TextBox1 to red color and bold.
}
有没有可以设置TextBox中文本属性的函数?
【问题讨论】:
标签:
c#
asp.net
colors
text-formatting
【解决方案1】:
TextBox1.ForeColor = Color.Red;
TextBox1.Font.Bold = True;
或者这可以使用 CssClass (推荐):
.highlight
{
color:red;
font-weight:bold;
}
TextBox1.CssClass = "highlight";
或者可以内联添加样式:
TextBox1.Attributes["style"] = "color:red; font-weight:bold;";
【解决方案2】:
尝试使用属性 ForeColor。
像这样:
TextBox1.ForeColor = Color.Red;
【解决方案3】:
string minusvalue = TextBox1.Text.ToString();
if (Convert.ToDouble(minusvalue) < 0)
{
// set color of text in TextBox1 to red color and bold.
TextBox1.ForeColor = Color.Red;
}
【解决方案4】:
另一种方法。这种方法可用于将文本更改为 2 种不同的颜色,只需添加 2 个跨度即可。
Label1.Text = "String with original color" + "<b><span style=""color:red;"">" + "Your String Here" + "</span></b>";
【解决方案5】:
试试这个:
label { color: red; font-weight:bold;}
label span { color: blue; font-weight:bold;}
<label>Label With Color <span>And Bold Text</span> </label>