【发布时间】:2018-03-02 09:18:17
【问题描述】:
我有一个搜索按钮“查找下一个”,它在 RichTextBox 中搜索,唯一的问题是,当我搜索“[e]”时,它会在 RichTextBox 中标记任何“e”。如果我搜索“[”,那么程序就会崩溃。这是我的代码:
private void downBtn_Click(object sender, EventArgs e)
{
string SearchWord = textBox1.Text;
if (SearchWord.Length > 0)
{
if (SearchWord != prevWord)
{
index = 0;
prevWord = SearchWord;
}
Regex reg = new Regex(SearchWord, RegexOptions.IgnoreCase);
foreach (Match find in reg.Matches(richTextBox1.Text))
{
if (find.Index >= index)
{
richTextBox1.Select(find.Index, find.Length);
richTextBox1.Focus();
index = find.Index + find.Length;
break;
}
}
}
}
【问题讨论】:
标签: c# regex winforms search richtextbox