如何在richtextbox函数中创建搜索文本,如texteditors搜索功能,在Windows窗体上 [英] How to make search text in richtextbox function, like texteditors search function, on windows forms
本文介绍了如何在richtextbox函数中创建搜索文本,如texteditors搜索功能,在Windows窗体上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的解决方案找到并滚动到找到的文本,但它不会选择所有搜索到的单词
这些只选择一个单词
我尝试过:
my solution find and scroll to that found text but it doesn't selects all searched words
these select just a word
What I have tried:
richTextBox1.Focus();
int start = richTextBox1.Text.IndexOf(textBox1.Text[0]);
int end = textBox1.Text.Length;
richTextBox1.Select(start, end);
推荐答案
String.IndexOf只查找字符串中的第一个匹配项,因此您需要使用重载重复搜索方法。
String.IndexOf finds only the first occurrence in the string, so you need to repeat teh search using the overloaded methods.
public static class MyExtensions
{
public static List<int> AllIndexesOf(this string s, string lookFor)
{
List<int> indexes = new List<int>();
int index = s.IndexOf(lookFor);
int len = lookFor.Length;
while (index >= 0)
{
indexes.Add(index);
index = s.IndexOf(lookFor, index + len);
}
return indexes;
}
}
将全部找到它们:
List<int> x = "abcdabceab".AllIndexesOf("abc");
将返回两个索引的列表。
但是......你可以不要选择多个文本区域 - 您必须自己手动(并暂时)突出显示它们,或者像某些文本编辑器那样提供查找下一个功能。
will return a List of two indexes.
But ... you can't select more than one text area - you will have to manually (and temporarily) highlight them yourself, or provide a "find next" function as some text editors do.
这篇关于如何在richtextbox函数中创建搜索文本,如texteditors搜索功能,在Windows窗体上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文