$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
Windows Forms RichTextBox Control
Edit rich text content with text formatting options, mail-merge, table, and floating objects, etc.
Home > WinForms UI Controls > User Manual > Set RichTextBox Control in C#

Settings of RichTextBox Control for C# Windows Forms

KetticRichTextBox control for Windows Forms is capable of the import and export of documents in various formats, including rtf, html, xaml, docx, plain text and pdf. To set the contents of the documents, we should clear the format of the data in the document. KetticRichTextBox control contains various format providers for the users to deal with the import and export of the documents. Additionally, this controls also allows the users to set the margins of a documents.

Get and Set the Text of KetticRichTextBox

To get and set the text of KetticRichTextBox control, we can use the format providers and assemblies included in the component listed as below.
  • TxtFormatProvider - Kettic.WinForms.RichTextBox.FormatProviders.Txt
  • DocxFormatProvider - Kettic.WinForms.RichTextBox.FileFormats.OpenXml.Docx
  • XamlFormatProvider - Kettic.WinForms.RichTextBox.FileFormats.Xaml
  • PdfFormatProvider - Kettic.WinForms.RichTextBox.FileFormats.Pdf
  • HtmlFormatProvider - Kettic.WinForms.RichTextBox.FileFormats.Html
  • RtfFormatProvider - Kettic.WinForms.RichTextBox.FileFormats.Rtf
To get the content of the document in a specific format, we shall create an instance of the corresponding provider and export the document. The C# code below shows how to export an plain text file.

public string GetTxt(KetticDocument document)
{
TxtFormatProvider provider = new TxtFormatProvider ();
return provider.Export(document);
}
We can also use the same way to set the content of KetticRichTextBox when the content is in one of the formats. The C# code below demonstrates how to import an html string in the document of KetticRichTextBox.

public KetticDocument ImportHtml(string content)
{
HtmlFormatProvider provider = new HtmlFormatProvider();
return provider.Import(content);
}

KetticRichTextBox control provides the Insert methods for KetticDocument and KetticRichTextBox to save the initial document content and insert text at different positions in the document. The Insert method applies the current span style of the document and the KetticDocument method allows the users to create a StyleDefinition and fill it up with the property values of the text. The following are the C# code examples.

C# Insert Text in KetticRichTextBox at Caret Position


this.ketticRichTextBox1.Insert(textToInsert);

C# Insert Text in a Custom Style using method of KetticDocument


private void InsertTextInDocument(string textToInsert)
{
StyleDefinition style = new StyleDefinition();
style.SetPropertyValue(Span.FontFamilyProperty, " Courier New");
style.SetPropertyValue(Span.FontSizeProperty, Unit.PointToDip(12));
style.SetPropertyValue(Span.ForeColorProperty, Color.Green);
this.ketticRichTextBox1.Document.Insert(textToInsert, style);
}

C# Insert Text in Current Editing Style of the RichTextBox


private void InsertTextInDocumentInCurrentSpanStyle(string textToInsert)
{
StyleDefinition style = new StyleDefinition();
style.CopyPropertiesFrom(this.ketticRichTextBox1.CurrentEditingStyle);
this.ketticRichTextBox1.Document.Insert(textToInsert, style);
}

C# Set Margins of the Document


ketticRichTextBox1.Document.LayoutMode = DocumentLayoutMode.Paged;
ketticRichTextBox1.Document.SectionDefaultPageMargin = new System.Windows.Forms.Padding(100);
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls