r
i
c
h
T
e
x
t
B
o
x
richTextBox
richTextBox
显示不同的颜色的文字
方式一:
public void Disply(ref RichTextBox rtb, string strInput, Color fontColor)
{
int p1 = rtb.TextLength;
rtb.AppendText(strInput + "\n");
int p2 = strInput.Length;
rtb.Select(p1, p2);
rtb.SelectionColor = fontColor;
}
private void button2_Click(object sender, EventArgs e)
{
Disply(ref this.richTextBox1, "Hi Major!", Color.Red);
}
public void Disply(ref RichTextBox rtb, string strInput, Color fontColor)
{
int p1 = rtb.TextLength;
rtb.AppendText(strInput + "\n");
int p2 = strInput.Length;
rtb.Select(p1, p2);
rtb.SelectionColor = fontColor;
}
方式二:
private void button1_Click_1(object sender, EventArgs e)
{
string strChars = richTextBox1.Text;
int iOldLength=0;
iOldLength = richTextBox2.Text.Length;
richTextBox2.AppendText(Environment.NewLine + strChars);
if (strChars == "ni")
{
richTextBox2.Select(iOldLength, richTextBox2.Text.Length);
richTextBox2.SelectionColor = Color.Red;
}
else
{
richTextBox2.Select(iOldLength, richTextBox2.Text.Length);
richTextBox2.SelectionColor = Color.Black;
}
}
}