How to Type Bold Text into a RichTextBox on a Silverlight Page by Clicking a Button1 on the same Page

I have a simple Silverlight application that has a Page1.xaml . This Page1.xaml has got a RichTextBox1 and a Button1 on it.
I would like to type Hello into this RichTextBox1 by clicking Button1. The following code types “Hello” for me in RichTextBox1 in regular font. I would like “Hello” to be typed in Bold. I would be grateful for modification of this code which will achieve this for me. Please note I am using Silverlight 4 in Visual Studio 2010. Thank you for your help in anticipation.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
txtNotes.Selection.Text = "Hello"

End Sub
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

You can use:

txtNotes.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold)
FaheemAhmadGulAuthor Commented:
Thank you for your comment. The code suggested however does not work.
I have tried the following
   txtNotes.FontWeight = FontWeights.Bold
   txtNotes.Selection.Text = "Hello"

The above does work however it sets the font of all of the text in txtNotes as Bold, where as I wish to only make the font that is typed in response to the click event of the button to be typed in bold and leave the previously typed text in the txtNotes as regular font.
Hi ,
I think that my suggestion should work, so I don't think you should give up so quickly.
Do you have some kind of an error? Exception?
Is it possible that you tried to insert ApplyPropertyValue statement before you set the Text? or Selection?
Try to check the value of Selection in the debugger when you come to the line I suggested...
Good luck anyway.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

FaheemAhmadGulAuthor Commented:
I am actually looking an equivalent of the following code from a Windows Forms Application.

txtNotes.SelectionFont = New Font(txtNotes.Font, FontStyle.Bold)
txtNotes.SelectedText = “This is bold text.”
txtNotes.SelectionFont = New Font(txtNotes.Font, FontStyle.Regular)
txtNotes.SelectedText = “This is regular text.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

txtNotes.SelectionFont = New Font(txtNotes.Font, FontStyle.Bold)
txtNotes.SelectedText = “This is bold text.”
txtNotes.SelectionFont = New Font(txtNotes.Font, FontStyle.Regular)
txtNotes.SelectedText = “This is regular text.”

' I am looking equivalent of the above code for Silverlight
End Sub

Open in new window

FaheemAhmadGulAuthor Commented:
The code suggested by grishave

txtNotes.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold)

does not give any error, but it just does not make the text typed subsequently bold.
But have you checked the value of Selection in the debugger ?
Also try to change the order - first set the text, and then FontWeight...
FaheemAhmadGulAuthor Commented:
I have tried setting the text first and then using the line of code suggested but it still does not have any effect on the text. However, even if it woroked in this way it would not solve my problem because I only want to make bold the text that is typed after the line of code.
I tried setting the text first and then adding the following line
txtNotes.Selection.Text = "Hello"
txtNotes.SelectAll ()
And  then putting the line of code suggested by you after this and it does then work, but for obvious reasons all the text in txtNotes then become bold, which as I said earlier is not the objective.
I don't quite understand what yo're trying to reach.
You said at the beginning that you want to set the text when user clicks the button.
Now I understand that probably you want to change only the part of the text...
Anyway, I think that what you need is to work with Paragraph object and not with Selection.
Take a look at this article - I think they have there exact example of what you want to achieve (scroll to the end of article).
Good luck.

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:
Many thanks for your help. I am very grateful.
My apologies for any convenience caused from initial description of what I wanted to achieve.
The code sample I provided for the Click Event of a Windows Forms application, perhaps clarifies what I wish to achieve and it is basically typing and (at the same time) formatting text through the click event of a Button.
In a Windows Forms application the code for formatting part or the whole of the text in a RichTextBox is nearly the same and is much simpler and I was hoping it would be something similar in Silverlight. It now seems that the way to achieve formatting only part of the text in a Silverlight RichTextBox is much more complex. The MSDN source you have pointed out does solve my problem though I do wish and wonder if there is a simpler method like in a Windows Forms application, where you write a simple line of code like
txtNotes.SelectionFont = New Font(txtNotes.Font, FontStyle.Bold)

and then anything else you type into the RichTextBox programmatically gets typed in Bold Text, until you use a line of code to like the following to return to normal regular text typing.
txtNotes.SelectionFont = New Font(txtNotes.Font, FontStyle.Regular)

Thank you very much again for all your help. Regards
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.