Click here to Skip to main content
16,020,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have one rich text box, one text box and one button, suppose there is word in text box and in rich text box there are 5 sentences (one sentence in a one line)
when i click on button so i want how many words are in rich text box first sentences and search word "highlight" and than cursor goes go next line to find the word.

Thanks in advance.
Posted
Comments
mayankshrivastava 20-Jan-12 9:11am    
i am using this code
<pre lang="c#">
if (txtsearchgerman.Text.Length > 0)
{
startindex = FindMyText(txtsearchgerman.Text.Trim(), start, txtshowfile.Text.Length);
// If string was found in the RichTextBox, highlight it
if (startindex >= 0)
{
// Set the highlight color as red
//txtshowfile.SelectionColor = Color.Red;
txtshowfile.SelectionBackColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtsearchgerman.Text.Length;
// Highlight the search string
txtshowfile.Select(startindex, endindex);
// mark the start position after the position of
// last search string
start = startindex + endindex;
}

public int FindMyText(string TxtToSearch, int searchStart, int searchEnd)
{
// Unselect the previously searched string
if (searchStart > 0 && searchEnd > 0 && indexofsearchtext >= 0)
{
txtshowfile.Undo();
//txtenglishtext.Undo();
}
// Set the return value to -1 by default.
int retvalue = -1;
// A valid starting index should be specified.
// if indexOfSearchText = -1, the end of search
if(searchStart >= 0 && indexofsearchtext >=0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexofsearchtext = txtshowfile.Find(TxtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
//indexofsearchtext = txtenglishtext.Find(TxtToSearch,searchStart,searchEnd,RichTextBoxFinds.None);
// Determine whether the text was found in richTextBox1.
if (indexofsearchtext != -1)
{
// Return the index to the specified search text.
retvalue = indexofsearchtext;
}
}
}
return retvalue;
}
</pre>

it works but now i want to search sentence by sentences and also count the word how many times occurs.

 
Share this answer
 
v2
To Highlight a word use SelectionStart and SelectionEnd Property to select the word.
To change the font use SelectionFont Property
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900