Social Icons

Thursday, 8 August 2013

How to add, read, clear the text content in richtextbox control in wpf

How to get, set, clear the text in richtextbox control in wpf


In this article you will know 
  • how to assign the text to richtextbox in wpf,
  •  how to read and clear the text from richtextbox, 
  • how to enable the spell check for richtextbox in wpf, 
  • what is the replacement text property for richtextbox,
  •  how to set the margins for richtextbox paragraphs. 


What is the replacement Text property for RichTextBox in WPF:

          Unlike other controls in wpf, richtextbox doesn't have content as property for setting content. Richtextbox has Document property to set or get the content or text. Here this Document is FlowDocument type. Here this FlowDocument may contain multiple paragraphs.

How to add text to richtextbox control:

    As the Document Property of the RichTextBox is the FlowDocument type so we have create an object of type FlowDocument, and add paragraphs to Blocks property of FlowDocument and Finally assign this FlowDocument to Document Property of RichTextBox Control.

Here is the Code showing how to add text to richtextbox control in WPF using C#.Net

 Vb.Net Code for addding text to richtextbox

  How to read Text from RichTextBox Control:

       To read text from RichTextBox FlowDocument type we have to use TextRange( ).

Code to Read text RichTextBox using C#.Net

Vb.Net Code

How to Clear Text in RichTextBox:

          While Adding text to richtextbox, we added paragraphs to Blocks property of FlowDocument type, so for clearing text we have to clear the Blocks of Document in the following two ways.

C#.Net  Code:

Vb.Net Code:

Setting Margin for RichTextBox paragraphs:

       While adding text using paragraphs we may get more space between para's in richtextbox so to set the margin between paragraphs use below code in resources section of XAML code.

Enabling Spell Check:

RichTextBox has spell check feature this can be enabled by setting 

richTextBox1.SpellCheck.IsEnabled = true;

You can find the video explained with example on how to add, read,  clear the content in wpf richtextbox control


I hope you are clear about reading, adding ,clearing the text from richtextbox control. I hope you will see the WPF Tutorials section for More Tutorials and How to's .

          Don't forget to Comment and Share if you like this post for our friends. Thank You.

1 comment:

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.