Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write check if a TextBlock consists a specific string in windows phone 7 XAML?

I've have a textblock and a textbox, I need to check and whether the textblock contains the textbox value, if yes then that specific value should be highlighted in some color. . .

For example,

TextBlock.Text="Just a Test"

if we type "te" in TextBox, then the value in the textblock should be highlight as "Just a *Te*st"

in xaml.

if anybody know means please say!

Thanks in Advance!

like image 408
Gopinath Perumal Avatar asked Jan 14 '23 16:01

Gopinath Perumal


1 Answers

Heres a sample code to be put in the text box TextChange event

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        string text = "this is a TextBlock";
        textBlock1.Text = text;
        textBlock1.Inlines.Clear();

        int index = text.IndexOf(textBox1.Text);
        int lenth = textBox1.Text.Length;


        if (!(index < 0))
        {
            Run run = new Run() { Text = text.Substring(index, lenth), FontWeight = FontWeights.ExtraBold };
            run.Foreground = new SolidColorBrush(Colors.Orange);
            textBlock1.Inlines.Add(new Run() { Text = text.Substring(0, index), FontWeight = FontWeights.Normal });
            textBlock1.Inlines.Add(run);
            textBlock1.Inlines.Add(new Run() { Text = text.Substring(index + lenth), FontWeight = FontWeights.Normal });

            textBlock1.FontSize = 30;
            textBlock1.Foreground = new SolidColorBrush(Colors.White);
        }
        else 
        {
            textBlock1.Text = "No Match";
        }
    }

this will highlight the text and return no match is no match is found.

note : this code snippet is case sensitive.

like image 166
Anobik Avatar answered Jan 17 '23 04:01

Anobik