设置RichTextBox部分字体
时间:2011-06-01 来源:xcanel
#region 设置消息显示加粗
/// <summary>
/// 设置消息显示加粗
/// </summary>
/// <param name="textBox"></param>
/// <returns></returns>
public RichTextBox SetFontColor(RichTextBox textBox)
{
//定义热点关键字
List<string> lsCommand = new List<string>();
lsCommand.Add("热点1");
lsCommand.Add("热点2");
for (int ICmdPra = 0; ICmdPra < lsCommand.Count; ICmdPra++)
{
try
{
if (!textBox.Text.Contains(lsCommand[ICmdPra]))
continue;
string[] list = new string[1];
list[0] = lsCommand[ICmdPra];
string[] hotPoint = textBox.Text.ToLower().Split(list, System.StringSplitOptions.RemoveEmptyEntries);
//字体设置起始位置
int setIndex = 0;
for (int hotIndex = 0; hotIndex < hotPoint.Length; hotIndex++)
{
setIndex = setIndex + hotPoint[hotIndex].Length;
//设置关键字颜色
textBox.Select(setIndex, lsCommand[ICmdPra].Length);
textBox.SelectionColor = Color.Red;
textBox.SelectionFont = new Font(textBox.Font, textBox.Font.Style | FontStyle.Bold);
setIndex = setIndex + lsCommand[ICmdPra].Length;
}
}
catch (Exception ex)
{
AppLog.Write("LiNing.Client.FrmMain.backgroundWorker1_DoWork", AppLog.LogMessageType.Fatal, ex);
}
}
return textBox;
}
#endregion