HI all:
i've tried to make a simple notepad in c# but i want to know how can i find a word in the text (i'm using Rich text box)
pleas help me???
HI all:
i've tried to make a simple notepad in c# but i want to know how can i find a word in the text (i'm using Rich text box)
pleas help me???
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Mar 20, 2008, 8:33 AM EDT
To find only complete words (as opposed to words embedded in other words) regular expressions are your best option. Here's some code which looks for all occurrences (ignoring case) within the RTB of a word entered in an (ordinary) textbox and then emboldens them:
using
System.Text.RegularExpressions;.......
string
word = textBox1.Text; // say Regex r = new Regex(@"\b" + word + @"\b", RegexOptions.IgnoreCase); MatchCollection mc = r.Matches(richTextBox1.Text); foreach (Match m in mc){
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = word.Length;
richTextBox1.SelectionFont =
new Font(richTextBox1.Font, FontStyle.Bold);richTextBox1.SelectionLength = 0;
}
Omar Mardini8 reputationPosted Mar 21, 2008, 1:16 PM EDT
Thanks Alan it works but if u can post how can i find a part of word and mark the word wich i found
thanks
AlanRank #669 · 3.6K reputationPosted Mar 21, 2008, 4:25 PM EDT
To find embedded words as well as complete words, just remove the two word boundary metacharacters \b from the regular expression:
Regex r = new Regex( word , RegexOptions.IgnoreCase);