【发布时间】:2014-10-31 14:48:25
【问题描述】:
我有一个方法可以检测用户名,突出显示并更改其颜色,然后将其放入 RichTextBox:
private void displayMessage(string message, string color)
{
string username = message.Substring(0, message.IndexOf(':') - 1);
string realMessage = message.Replace(username , "");
serverChat.Text += message;
serverChat.Select(((serverChat.Text.Length - message.Length) - 1), username.Length - 1);
serverChat.SelectionColor = getColor(color);
serverChat.Text += Environment.NewLine;
}
private void sendButton_Click(object sender, EventArgs e)
{
displayMessage("PersonX: Hey is it working for you?", "1");
displayMessage("PersonY: Yeah, it just started. Thanks!", "0");
}
private Color getColor(string index)
{
switch (index)
{
case "0":
return Color.Red;
case "1":
return Color.Blue;
case "2":
return Color.Green;
case "3":
return Color.Yellow;
case "4":
return Color.Black;
default:
return Color.Black;
}
}
结果是我得到的所有文本都变回了红色。有什么我做错了或有什么原因导致它无法正常工作吗?
【问题讨论】:
-
我想这是关于 Winforms 的。你的问题解决了吗?
标签: c# colors richtextbox