Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all text in multiline wpf textblock

Tags:

wpf

I have Multiline TextBlock, I want to get all it's lines by code Can someone help me?

The TextBlock:

<TextBlock Name="tb" TextWrapping="Wrap" >
                              Name:_____________
                                <LineBreak/>
                                 Mark:____________
          </TextBlock>

In C#:

text = ((TextBlock)tb).Text;

But I got only the first line.

Thanks!

like image 936
mich Avatar asked Oct 30 '25 09:10

mich


2 Answers

You can try this:

StringBuilder s = new StringBuilder();
foreach (var line in tb.Inlines)
{
    if (line is LineBreak)
        s.AppendLine();
    else if (line is Run)
        s.Append(((Run) line).Text);
}
var text = s.ToString();

Found it here

like image 144
Johan Larsson Avatar answered Nov 01 '25 13:11

Johan Larsson


If you want to display on multiple lines you can use :

<TextBlock Name="myText" Text="I go &#x0a; Home " >

and sure, you can get all lines by parsing the string.

like image 40
Dragos Stoica Avatar answered Nov 01 '25 12:11

Dragos Stoica