1 minute read

I was trying my hands on Silverlight 4 Beta version and found this is wonderful control - RichTextBox.. This was much needed on Web Platform and Microsoft has really made it work simple... Rather, thanks to the invention of XAML!

This small control supports many features. Some of them are:

  • Formatting of text at runtime - font, color, bold, italics, underline, etc.
  • Addition of inline UI Elements in the Text Box itself.
  • Read / Write only modes both supported.
  • Hyperlinks can also be inserted in the text.

Let's do some scripting. Two ways to work:-

Way 1: Texting through XAML

[sourcecode language='xml']
This is a sample text.
[/sourcecode]

[sourcecode language='xml']

This is a sample text.

[/sourcecode]

Way 2: Texting through C# Code

[sourcecode language='csharp']

// Create a Run of plain text and some bold text.

Run myRun1 = new Run();

myRun1.Text = "This is a ";

Bold myBold = new Bold();

myBold.Inlines.Add("sample");

Run myRun2 = new Run();

myRun2.Text = "text.";

// Create a paragraph and add the Run and Bold to it.

Paragraph myParagraph = new Paragraph();

myParagraph.Inlines.Add(myRun1);

myParagraph.Inlines.Add(myBold);

myParagraph.Inlines.Add(myRun2);

// Add the paragraph to the RichTextArea.

richTextBox.Blocks.Add(myParagraph);

//Add the RichTextArea to the StackPanel.

stackPanel.Children.Add(richTextBox);

[/sourcecode]

Hope this helps!