C# 突出显示所有搜索的单词
在我的C# 突出显示所有搜索的单词,c#,winforms,find,richtextbox,highlight,C#,Winforms,Find,Richtextbox,Highlight,在我的RichtextBox中,如果我写了以下内容 这是我的钢笔, 他的钢笔很漂亮 现在我搜索“是”这个词 输出如下 所有的“是”都应该突出显示。看起来这样就可以了 那么: static class Utility { public static void HighlightText(this RichTextBox myRtb, string word, Color color) { if (word == string.Empty) re
RichtextBox
中,如果我写了以下内容
这是我的钢笔,他的钢笔很漂亮 现在我搜索“是”这个词 输出如下
所有的“是”都应该突出显示。看起来这样就可以了 那么:
static class Utility {
public static void HighlightText(this RichTextBox myRtb, string word, Color color) {
if (word == string.Empty)
return;
int s_start = myRtb.SelectionStart, startIndex = 0, index;
while((index = myRtb.Text.IndexOf(word, startIndex)) != -1) {
myRtb.Select(index, word.Length);
myRtb.SelectionColor = color;
startIndex = index + word.Length;
}
myRtb.SelectionStart = s_start;
myRtb.SelectionLength = 0;
myRtb.SelectionColor = Color.Black;
}
}
这将同时显示所有搜索的条件 使用:1个文本框(输入要搜索的文本)和1个按钮(运行搜索) 在文本框中输入搜索条件,然后按搜索按钮
// On Search Button Click: RichTextBox ("rtb") will display all the words inside the document
private void btn_Search_Click(object sender, EventArgs e)
{
try
{
if (rtb.Text != string.Empty)
{// if the ritchtextbox is not empty; highlight the search criteria
int index = 0;
String temp = rtb.Text;
rtb.Text = "";
rtb.Text = temp;
while (index < rtb.Text.LastIndexOf(txt_Search.Text))
{
rtb.Find(txt_Search.Text, index, rtb.TextLength, RichTextBoxFinds.None);
rtb.SelectionBackColor = Color.Yellow;
index = rtb.Text.IndexOf(txt_Search.Text, index) + 1;
rtb.Select();
}
}
}
catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
}
}
//在搜索按钮上单击:RichTextBox(“rtb”)将显示文档中的所有单词
私有无效btn\u搜索\u单击(对象发送者,事件参数e)
{
尝试
{
if(rtb.Text!=string.Empty)
{//如果ritchtextbox不是空的,则突出显示搜索条件
int指数=0;
字符串temp=rtb.Text;
rtb.Text=“”;
文本=温度;
while(索引
}如果您只想匹配整个单词,您可以使用此选项,请注意,此选项忽略大小写,并且| s\b表示复数形式突出显示,例如Cat匹配Cat,但不匹配caterpiller:
public static void HighlightText(RichTextBox myRtb, string word, Color color)
{
if (word == string.Empty)
return;
var reg = new Regex(@"\b" + word + @"(\b|s\b)",RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(myRtb.Text))
{
myRtb.Select(match.Index, match.Length);
myRtb.SelectionColor = color;
}
myRtb.SelectionLength = 0;
myRtb.SelectionColor = Color.Black;
}
private void按钮3\u单击(对象发送者,事件参数e)
{
如果(textBox1.Text!=“”)
{
对于(int i=0;i
我会这样做,因为所有其他答案都会突出显示文本,但在您再次搜索后不会更改回文本
使用RichText Find方法查找搜索词的起始索引
public int FindMyText(string searchText, int searchStart, int searchEnd)
{
int returnValue = -1;
if (searchText.Length > 0 && searchStart >= 0)
{
if (searchEnd > searchStart || searchEnd == -1)
{
int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
if (indexToText >= 0)
{
returnValue = indexToText;
}
}
}
return returnValue;
}
使用按钮或TextChangeListener搜索您的单词
private void button1_Click(object sender, EventArgs e)
{
// Select the first char in your Richtextbox
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.TextLength;
// Select until the end
richTextBox1.SelectionColor = Color.Black;
// Make the Text Color black
//Use an Inputfield to add the searching word
var word = txtSearch.Text;
//verify the minimum length otherwise it may freeze if you dont have text inside
if (word.Length > 3)
{
int s_start = richTextBox1.SelectionStart, startIndex = 0, index;
while ((index = FindMyText(word, startIndex, richTextBox1.TextLength)) != -1)
{
// goes through all possible found words and color them blue (starting index to end)
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = Color.Blue;
startIndex = index + word.Length;
}
// Color everything between in color black to highlight only found words
richTextBox1.SelectionStart = startIndex;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = Color.Black;
}
}
我强烈建议设置最小字长,以避免冻结和高内存分配。这将只找到第一次出现的字长,我想突出显示所有匹配的字长,因此“This”中的“is”和“his”也应突出显示。您能否澄清目标平台是否存在?WPF中有一个
RichTextBox
,可接受的解决方案不适用于该框。为什么要从startIndex位置搜索两次单个单词,并使用:while(myRtb.Text.IndexOf(word,startIndex)!=-1{int index=myRtb.Text.IndexOf(word,startIndex)
?我认为您应该将找到的索引保存在while循环中。请解释您的答案,而不仅仅是提供代码。此外,虽然您的解决方案似乎有效,但这个问题已经很老了,并且已经有了一个可接受的答案。对于较新的问题,您的帮助将更为感激:)好的,当然。感谢您能找到很多关于这个问题的信息站点在帮助中心工作,例如:如何回答:
public int FindMyText(string searchText, int searchStart, int searchEnd)
{
int returnValue = -1;
if (searchText.Length > 0 && searchStart >= 0)
{
if (searchEnd > searchStart || searchEnd == -1)
{
int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
if (indexToText >= 0)
{
returnValue = indexToText;
}
}
}
return returnValue;
}
private void button1_Click(object sender, EventArgs e)
{
// Select the first char in your Richtextbox
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.TextLength;
// Select until the end
richTextBox1.SelectionColor = Color.Black;
// Make the Text Color black
//Use an Inputfield to add the searching word
var word = txtSearch.Text;
//verify the minimum length otherwise it may freeze if you dont have text inside
if (word.Length > 3)
{
int s_start = richTextBox1.SelectionStart, startIndex = 0, index;
while ((index = FindMyText(word, startIndex, richTextBox1.TextLength)) != -1)
{
// goes through all possible found words and color them blue (starting index to end)
richTextBox1.Select(index, word.Length);
richTextBox1.SelectionColor = Color.Blue;
startIndex = index + word.Length;
}
// Color everything between in color black to highlight only found words
richTextBox1.SelectionStart = startIndex;
richTextBox1.SelectionLength = 0;
richTextBox1.SelectionColor = Color.Black;
}
}