C# 在文本框中搜索并突出显示文本
我试图在我的程序中做一些搜索功能,只是突出显示在textbox上找到的所有关键字 关键字框位于t2.text上,文本来自bx2.text 首先,我在这里尝试了这种方法 正则表达式正在工作,但突出显示不工作 这里有什么问题吗C# 在文本框中搜索并突出显示文本,c#,C#,我试图在我的程序中做一些搜索功能,只是突出显示在textbox上找到的所有关键字 关键字框位于t2.text上,文本来自bx2.text 首先,我在这里尝试了这种方法 正则表达式正在工作,但突出显示不工作 这里有什么问题吗 private void searchs() { var spattern = t2.Text; foreach(var s in bx2.Text) { var zxc = s.ToS
private void searchs()
{
var spattern = t2.Text;
foreach(var s in bx2.Text)
{
var zxc = s.ToString();
if (System.Text.RegularExpressions.Regex.IsMatch(zxc, spattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
bx2.Text.Replace(bx2.Text, @"<span class=""search-highlight"">$0</span>");
}
}
}
private void search()
{
var spattern=t2.Text;
foreach(bx2.Text中的变量s)
{
var zxc=s.ToString();
if(System.Text.RegularExpressions.Regex.IsMatch(zxc、spattern、System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
替换(bx2.Text,@“$0”);
}
}
}
链接的问题使用HTML格式,这在常规文本框中是无法使用的
您需要一个文本框。在那里,您可以使用控件的方法和属性格式化文本(不使用HTML)
或者使用浏览器控件而不是文本框来使用HTML格式。private void findButton\u单击(对象发送方,事件参数e)
private void findButton_Click(object sender, EventArgs e)
{
int count = 0;
string keyword = keywordTextbox.Text.Trim();//getting given searching string
int startPosition = 0; //initializing starting position to search
int endPosition = 0;
int endArticle = articleRichTextbox.Text.Length;//finding total number of character into the article
for (int i = 0; i<endArticle ; i =startPosition )//creating a loop until ending the article
{
if (i == -1) //if the loop goes to end of the article then stop
{
break;
}
startPosition = articleRichTextbox.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);//using find method get the begining position when find the searching string
if (startPosition >= 0) //if match the string //if don't match the string then it return -1
{
count++;//conunting the number of occuerence or match the search string
articleRichTextbox.SelectionColor = Color.Blue;//coloring the matching string in the article
endPosition = keywordTextbox.Text.Length;
startPosition = startPosition + endPosition;//place the starting position at the next word of previously matching string to continue searching.
}
}
if (count == 0)//if the givn search string don't match at any time
{
MessageBox.Show("No Match Found!!!");
}
numberofaccurTextbox.Text = count.ToString();//show the number of occurence into the text box
}
{
整数计数=0;
string关键字=keywordTextbox.Text.Trim();//获取给定的搜索字符串
int startPosition=0;//初始化要搜索的起始位置
int-endPosition=0;
int endArticle=articleRichTextbox.Text.Length;//查找文章中的字符总数
for(int i=0;i=0)//如果匹配字符串//如果不匹配字符串,则返回-1
{
count++;//计算发生次数或匹配搜索字符串
articleRichTextbox.SelectionColor=Color.Blue;//为文章中的匹配字符串着色
endPosition=关键字TextBox.Text.Length;
startPosition=startPosition+endPosition;//将起始位置放在先前匹配字符串的下一个单词处,以继续搜索。
}
}
if(count==0)//如果givn搜索字符串在任何时候都不匹配
{
MessageBox.Show(“未找到匹配项!!!”;
}
numberofaccurTextbox.Text=count.ToString()//在文本框中显示发生次数
}
RichTextBox确实是基本的.Net控件之一,因此这将是一个不错的选择。