编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

c# RichText 指定批量修改指定文字属性

wxchong 2024-07-16 10:08:51 开源技术 33 ℃ 0 评论

/// <summary>

/// 加黑关键字

/// </summary>

private void blod()

{

#region 读取Bold.txt文件中的文字并 加红关键字

string txtpath = System.Windows.Forms.Application.StartupPath + @"\Bold.txt";

List<string> list = SqlHelper.ReadTxt(txtpath);

string[] arr = list.ToArray();

arr = arr.Where(s => !string.IsNullOrEmpty(s)).ToArray();//过滤空字符串

for (int i = 0; i < arr.Length; i++)

{

string FindWord = arr[i];

if (FindWord != "")

{

changeColor(rtxtBz, FindWord);

}

}

#endregion

list.Clear();

}

#region 指定关键字颜色

/// <summary>

/// 改变richTextBox中指定字符串的颜色

/// 调用即可

/// </summary>

/// <param name="str" value="为指定的字符串"></param>

public void changeColor(RichTextBox rich, string str)

{

ArrayList list = getIndexArray(rich.Text, str);

for (int i = 0; i < list.Count; i++)

{

int index = (int)list[i];

rich.Select(index, str.Length);

rich.SelectionColor = Color.Green;

}

list.Clear();

}

public ArrayList getIndexArray(String inputStr, String findStr)

{

ArrayList list = new ArrayList();

int start = 0;

while (start < inputStr.Length)

{

int index = inputStr.IndexOf(findStr, start);

if (index >= 0)

{

list.Add(index);

start = index + findStr.Length;

}

else

{

break;

}

}

return list;

}

#endregion

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表