我使用 C#(Windows 窗体)创建了类似记事本的应用程序,并且我想添加“查找”功能,该功能将突出显示搜索词的每个外观。但是我不知道如何添加到现有选择中,因此我最终只突出显示搜索词的最后一次出现。这是我的代码:
Regex regex = new Regex(args.searchTerm);
MatchCollection matches = regex.Matches(richTextArea.Text);
foreach (Match match in matches)
{
richTextArea.Select(match.Index, match.Length);
}
所以我该怎么做?
决定你做什么want:
例子:
private void searchTextBox_TextChanged(object sender, EventArgs e)
{
Regex regex = new Regex(searchTextBox.Text);
MatchCollection matches = regex.Matches(richTextArea.Text);
richTextArea.SelectAll();
richTextArea.SelectionBackColor = richTextArea.BackColor;
foreach (Match match in matches)
{
richTextArea.Select(match.Index, match.Length);
richTextArea.SelectionBackColor = Color.Yellow;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)