I want a text color to be red in color on certain condition.

Here is how i want to get it done.

string minusvalue = TextBox1.Text.ToString();
if (Convert.ToDouble(minusvalue) < 0)
{ 
// set color of text in TextBox1 to red color and bold.
}

Is there any function that can set the property of text in TextBox?

有帮助吗?

解决方案

TextBox1.ForeColor = Color.Red;
TextBox1.Font.Bold = True;

Or this can be done using a CssClass (recommended):

.highlight
{
  color:red;
  font-weight:bold;
}

TextBox1.CssClass = "highlight";

Or the styles can be added inline:

TextBox1.Attributes["style"] = "color:red; font-weight:bold;";

其他提示

Try using the property ForeColor. Like this :

TextBox1.ForeColor = Color.Red;
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;
}

Another way of doing it. This approach can be useful for changing the text to 2 different colors, just by adding 2 spans.

Label1.Text = "String with original color" + "<b><span style=""color:red;"">" + "Your String Here" + "</span></b>";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top