Reputation: 117
im trying to make some kind of search function in my program which is just highlighting all keywords found on textbox
the keyword box is on t2.text and the text is from bx2.text
firstly i tried out this method from here Highlight key words in a given search text
the regex is working but the highlight not
any wrong here?
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>");
}
}
}
Upvotes: 0
Views: 9281
Reputation: 31
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
}
Upvotes: 1
Reputation: 21878
The linked question uses HTML formatting, which you can't use in a regular textbox.
You need either a RichText box. In there you can format text using the control's methods and properties (not using HTML).
Or use a browser control rather than a textbox to use the HTML formatting.
Upvotes: 1