Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to find any word in textbox.Text.I have two textboxes,in first textbox i write any sentence, for example` Hi,how are you? and in second textbox i write any word, for example` "you",then when i click on button find, it must underline that word and say me that there is 1 word with Label.
Someone can help me how can i write this code with WPF.
Thank you.

What I have tried:

This is my code`
<Grid>
        <Button Click="button1_Click" x:Name="button1" Content="Find" Margin="185,180,146,140"/>
        <TextBox x:Name="textBox1" Margin="10,10,146,191"/>
        <TextBox x:Name="textBox2" Margin="10,180,296,140"/>
    </Grid>


private void button1_Click(object sender, RoutedEventArgs e)
       {
           int index = 0;
           string temp = textBox1.Text;
           textBox1.Text = "";
           textBox1.Text = temp;
           while(index < textBox1.Text.LastIndexOf(textBox2.Text))
           {
               textBox1.Select(index, textBox2.Text.Length);
               textBox1.SelectionBrush = Brushes.Red;
               index = textBox1.Text.IndexOf(textBox2.Text, index) + 1;
           }
       }



ScreenShot`
[^]
Posted
Updated 1-Apr-18 15:48pm
v3

Hello.

A good way to give you lots of flexibility of formatting is using the RichTextBox. See the following for tutorial: The RichTextBox control]
 
Share this answer
 
To see if a word exists, do this:

C#
bool found = textBox1.Text.ToLower().IndexOf("word") >= 0;


I have pretty much no idea what the rest of your question means.

I *do* know that you can't underline text in a textbox unless you create a custom control to do it.
 
Share this answer
 
v2
Comments
Suren97 28-Mar-18 13:25pm    
It should find that word then underline that word` SelectionBrush = Brushes.Blue; and in label it should say me that how many word in textbox there are.
Suren97 28-Mar-18 13:25pm    
I updated my code, but it's wrong
Richard Deeming 29-Mar-18 13:59pm    
You should normalize strings to upper-case, to avoid the "Turkish I" problem:
CA1308: Normalize strings to uppercase[^]
The Turkish İ Problem and Why You Should Care[^]

But in this case, there's no need to create a second copy of the string to perform a case-insensitive search. Just use the overload of IndexOf which takes a StringComparison[^]:
bool found = textBox1.Text.IndexOf("word", StringComparison.CurrentCultureIgnoreCase) >= 0;
Suren97 29-Mar-18 14:15pm    
I have already written this code, it was so simple` textBox1.Select(textBox1.Text.IndexOf(textBox2.Text), textBox2.Text.Length);
textBox1.Focus();
We did a coding challenge last year and I did something very similar to what you're asking for... check it out here: Coding challenge: find the repeated items in a collection of elements.[^]
 
Share this answer
 
Comments
#realJSOP 29-Mar-18 14:41pm    
He's not necessarily looking for duplicates. BTW, my solution to that challenge was not over-engineered enough to submit it. :)
Graeme_Grant 29-Mar-18 17:47pm    
LMAO...

I just read his comment to you about underlining the found words ... yes, the link to the challenge code is overkill but will show him how to underline words...
#realJSOP 30-Mar-18 9:30am    
I had a change of heart, and submitted my solution anyway. :)

Coding challenge: find the repeated items in a collection of elements.[^]
Graeme_Grant 30-Mar-18 12:40pm    
:)

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