Using <Run> Programmatically
Last post 05-09-2008 6:25 PM by evo9. 4 replies.
Sort Posts:
05-08-2008 1:20 PM
Using <Run> Programmatically

Can someone shed some light on why this doesn't work:

            StringBuilder _chat = new StringBuilder(txtChatOut3.Text);
            Run _r = new Run();
            _r.Foreground = new SolidColorBrush(Colors.Purple) ;
            _r.Text = "groovy";
            _chat.Insert(0, "Ben: I want my diet coke now!!!\nBen:" + _r.Text + "\n");
            txtChatOut3.Text = _chat.ToString();

 I'm just trying to color a single word in the middle of a larger textblock programmatically...

 


 

evo9

Joined on 04-30-2008
Posts 8
05-09-2008 4:56 AM
Re: Using <Run> Programmatically

once you do

_chat.Insert(0, "Ben: I want my diet coke now!!!\nBen:" + _r.Text + "\n");

all the formatting will be lost. Try using Inlines property

lee_sl

Joined on 05-09-2008
Posts 10
05-09-2008 12:48 PM
Re: Using <Run> Programmatically

 How would I use inline to dyanically build a string of selected items from a user color coded?

 For example my client wants to select items from a list (a coke, an apple, a peach, his script, etc) and each item would be inserted into a chat out preview window with the items he's selected color coded. The list of items is coming from an xml file that he/his handlers can update.

 
I need to be able to dynamically build the string in code behind and color code only certain keywords that he selects. I don't see how i can use inline to do this...
 

evo9

Joined on 04-30-2008
Posts 8
05-09-2008 2:44 PM
Marked as Answer
Re: Re: Using &lt;Run&gt; Programmatically

initially assign some text to textblock(because of a bug)

//txt1 is name of the textblock

txt1.Text = "";//workaround for bugfor (int i = 0; i < 10; i++)

{

Run r = new Run();

if (i %2 == 0)

r.Foreground = new SolidColorBrush(Colors.Red);

else

r.Foreground = new SolidColorBrush(Colors.Blue);r.Text = "item" + i.ToString();

txt1.Inlines.Add(r);

}

 

lee_sl

Joined on 05-09-2008
Posts 10
05-09-2008 6:25 PM
Re: Re: Using &lt;Run&gt; Programmatically

 Thanks everyone, I got it mostly worked out. I'm new to silverlight so I appreciate the feedback on things like inline (which I'd never even heard of).

 

Here's how I ended up doing it if anyone else runs into this situation:

 

            StringBuilder _chat = new StringBuilder(txtChatOut3.Text);
            txtChatOut3.Text = _chat.ToString();

            Run _color = new Run();
            Run _white = new Run();

            _color = new Run();
            _color.Text = "peach\n";
            _color.Foreground = new SolidColorBrush(Color.FromArgb(255, 117, 189, 226));
            txtChatOut3.Inlines.Insert(0, _color);

            _white = new Run();
            _white.Text = " and a ";
            txtChatOut3.Inlines.Insert(0, _white);

            _color = new Run();
            _color.Text = "diet coke";
            _color.Foreground = new SolidColorBrush(Color.FromArgb(255, 117, 189, 226));
            txtChatOut3.Inlines.Insert(0, _color);

            _white = new Run();
            _white.Text = "Ben: I would like a ";
            txtChatOut3.Inlines.Insert(0, _white);

 

The trick is to build the sentence backwards. Otherwise, I wasn't able to insert the text in the top of the 'chat' window (normally I would have used a stringbuilder type of thing, but formatting is lost if you stuff it in there).

 

One odd thing, unless I do these lines at the top, very very odd things happen...

             StringBuilder _chat = new StringBuilder(txtChatOut3.Text);
            txtChatOut3.Text = _chat.ToString();

 

I'm not sure why I couldn't just insert using inline the colored text the way I need it without first loading the contents of the textblock again, but for now this works. The only bad thing is I lose the formatting for all the text that was in there, but for now I guess this is ok.

 Maybe I'm overlooking something... feedback welcome. But I think this is a bug, it's really really odd how the control acts if I don't reload the contents first.

 

Thanks all,

rick
 

 

evo9

Joined on 04-30-2008
Posts 8