Click here to Skip to main content
15,797,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XAML:
XML
<TextBlock x:Name="tb"/>


CS:
this.tb.Text="This is a string";
string str="Test";
this.tb.Inlines.Add(new Run(str));


Output:
"This is a stringTest"

But I want this,"This is a Teststring".

Can anyone tell me how to do,please?Thanks a lot!
Posted

1 solution

Something like this:

C#
string lineToAppend = //... new Run(str) is probably incorrect, but something method returning string
tb.Text = string.Format("{0}{1}{2}", tb.Text, System.Environment.NewLine, lineToAppend);


This is simple, but performance can be a problem if you do it repeatedly and and collect a lot of text (see below). Try this for better performance:

C#
//this is where performance is compromised,
//but at teast twice less than in previous sample:
tb.SelectionStart = tb.Text.Length;

tb.SelectionLength = 0;
tb.SelectedText = lineToAppend;


Unfortunately, text box types do not support access by line directly; and appending performance is not good (because every time you append the whole Text it is copied back and forth again and again). For equivalent of such functionality, use ListBox and add or modify items of string type.

C#
ListBox myListBox = new ListBox();

//...

myListBox.Items.Add(lineToAppend); //much faster; can hold many items without loss of performance


ListBox is much better then TextBox if you want appending some output results programmatically.

—SA
 
Share this answer
 
v5
Comments
tpywocao 7-Mar-11 2:49am    
Thanks a lot!I got it!
Sergey Alexandrovich Kryukov 7-Mar-11 4:27am    
Great. Will you formally accept my Answer? (A vote would not hurt, too.)
Good luck,
--SA

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