How to Type Green Colored Text into a RichTextBox on the same line (Without starting a new paragraph)

I have code for the click event of a Button1 on a Silverlight 4 page which types a sentence in green colored text into richTextBox on the page. If I click this button a second time, it again types green colored text into this richTextBox1 but this gets typed in a new paragraph.
There fore the result of clicking this Button1 is as follows:

This sentence is green color font.
This sentence is green color font.

I would instead like it to be as follows:

This sentence is green color font. This sentence is green color font.

I would be grateful for help with a modification of the code I am providing which will help me achieve this. Many thanks for your help in anticipation.
 
private void Button1_Click(object sender, RoutedEventArgs e)
        {


        Run myGreenRun = new Run();
        myGreenRun.Text = "This sentence is green color font. ";
        myGreenRun.Foreground = new SolidColorBrush(Colors.Green);
        Paragraph myParagraph = new Paragraph();
        myParagraph.Inlines.Add(myGreenRun);
        richTextBox1.Blocks.Add(myParagraph);
        richTextBox1.Focus ();


        TextSelection textRange = richTextBox1.Selection;
        textRange.Select(richTextBox1.ContentEnd, richTextBox1.ContentEnd);
        richTextBox1.Focus();
                                
        }

Open in new window

LVL 1
FaheemAhmadGulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

saraganiCommented:
private void Button1_Click(object sender, RoutedEventArgs e)
        {


        Run myGreenRun = new Run();
        myGreenRun.Text = "This sentence is green color font. ";
        myGreenRun.Foreground = new SolidColorBrush(Colors.Green);
        Paragraph myParagraph = new Paragraph();
        myParagraph.Inlines.Add(myGreenRun);
        richTextBox1.Blocks.Add(myParagraph);
        richTextBox1.Focus ();


        TextSelection textRange = richTextBox1.Selection;
        textRange.Select(richTextBox1.ContentEnd, richTextBox1.ContentEnd);
        richTextBox1.Focus();
                               
        }
0
saraganiCommented:
Oops sorry, ignore.. wrong paste. This is the right code:


        Run myGreenRun = new Run();
        myGreenRun.Text = "This sentence is green color font. ";
        myGreenRun.Foreground = new SolidColorBrush(Colors.Green);
       
        Paragraph myParagraph;

        if (richTextBox1.Blocks.Count > 0)
        {
            myParagraph = richTextBox1.Blocks[0] as Paragraph ;
        }
        else
        {
            myParagraph = new Paragraph();
        }
        myParagraph.Inlines.Add(myGreenRun);
        richTextBox1.Blocks.Add(myParagraph);
        richTextBox1.Focus ();


        TextSelection textRange = richTextBox1.Selection;
        textRange.Select(richTextBox1.ContentEnd, richTextBox1.ContentEnd);
        richTextBox1.Focus();
                               
     
0
FaheemAhmadGulAuthor Commented:
Thank you for your prompt response to my request for help.
The code as suggested gives the following error

Element is already the child of another element, with the following line in the code highlighted

richTextBox1.Blocks.Add(myParagraph);
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

saraganiCommented:
Weird. it works here, but I've found out why, I was running FireFox. It seems that the IE silverlight plugin is written differently.

This code will also run with IE:

            Run myGreenRun = new Run();
            myGreenRun.Text = "This sentence is green color font. ";
            myGreenRun.Foreground = new SolidColorBrush(Colors.Green);
            bool newParagraph = false;

            Paragraph myParagraph;

            if (richTextBox1.Blocks.Count > 0)
            {
                myParagraph = richTextBox1.Blocks[0] as Paragraph;
            }
            else
            {
                myParagraph = new Paragraph();
                newParagraph = true;
            }
            myParagraph.Inlines.Add(myGreenRun);

            if (newParagraph)
                richTextBox1.Blocks.Add(myParagraph);
            richTextBox1.Focus();


            TextSelection textRange = richTextBox1.Selection;
            textRange.Select(richTextBox1.ContentEnd, richTextBox1.ContentEnd);
            richTextBox1.Focus();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FaheemAhmadGulAuthor Commented:
Brilliant!
This has worked perfectly. Many thanks.
0
saraganiCommented:
You are welcome :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.