[英]Setting Multiple textbox.SelectionStart values, to highlight searched words
将文本文件导入Windows窗体应用程序RTF框后,我现在要添加搜索功能。 是否可以有多个SelectionStart值? 看到相同的单词,SelectionLength将相同。
string textfield = TextField.Text;
string searchword = searchbox.Text;
int found=0;
TextField.SelectionLength = searchword.Length;
TextField.SelectionBackColor = Color.LightBlue;
for (int y = 0; y < textfield.Length; y++)//Goes through whole string
{
if (searchword[0] == textfield[y])//Looks for first character
{
for (int x = 0; x < searchword.Length; x++)//Checks if rest of characters match
{
if (searchword[x] == textfield[y + x])
{
found++;
}
else
break;
}
}
if (found == searchword.Length)
{
TextField.SelectionStart = y;//////Want to have multiple of these
}
found=0;
}
TextField.Focus();
不,你不能。 但是,例如,您可以更改所选文本的背景色。 首先选择一个单词。 然后做这个
foreach (Match match in matches) {
richTextBox.SelectionStart = match.Index;
richTextBox.SelectionLength = match.Length;
richTextBox.SelectionBackColor = Colors.Yellow;
}
要清除所有标记,只需选择整个文本,然后将背景色设置为白色(假设您不使用背景色)。
在示例中,我假设您正在使用Regex
。 您会发现以下单词:
string pattern = String.Format(@"\b{0}\b", Regex.Escape(wordToFind));
MatchCollection matches = Regex.Matches(richTextBox.Text, pattern);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.