Products Technologies Demo Docs Blog Support Company

RichTextBox Compatible Data Binding with TX Text Control

A frequent need when composing UIs is to bind a property to an information source to separate presentation and content (and logic). There are several ways to accomplish this task including MVC, MVVM and MVP. A core concept to realize those architectural concepts in Windows Forms (and WPF) is property binding. The .NET Framework System.Windows.Forms.RichTextBox provides an Rtf property that gets or sets the text of the RichTextBox control, including all rich text format (RTF) codes. This…

RichTextBox Compatible Data Binding with TX Text Control

A frequent need when composing UIs is to bind a property to an information source to separate presentation and content (and logic). There are several ways to accomplish this task including MVC, MVVM and MVP. A core concept to realize those architectural concepts in Windows Forms (and WPF) is property binding.

The .NET Framework System.Windows.Forms.RichTextBox provides an Rtf property that gets or sets the text of the RichTextBox control, including all rich text format (RTF) codes. This property accepts formatted text in the RTF format and can be used to bind the RichTextBox directly to data sources.

As TX Text Control supports many different file formats including DOC, DOCX, RTF and PDF, the file loading concept differs from the RichTextBox and is implemented using a central Load method to import various input streams.

But it is very easy to simulate the same property binding by subclassing TX Text Control and implementing your own Rtf property:

public class MyTextControl : TXTextControl.TextControl
{
    public MyTextControl()
    {
        this.CreateControl();
    }

    public string Rtf
    {
        get
        {
            string data;
            this.Save(out data, TXTextControl.StringStreamType.RichTextFormat);
            return data;
        }
        set
        {
            if (value != "")
                this.Load(value, TXTextControl.StringStreamType.RichTextFormat);
        }
    }
}

Now, the subclassed MyTextControl can be bound to a BindingSource in the same way like using the RichTextBox:

DataTable dt = new DataTable();
dt.Columns.Add("rtfText");

dt.Rows.Add(new string[] { @"{
tf1 Test \par Test}" });
dt.Rows.Add(new string[] { @"{
tf1 Test2 \par Test2}" });
dt.Rows.Add(new string[] { @"{
tf1 Test3 \par Test3}" });

bindingSource1.DataSource = dt;

myTextControl1.DataBindings.Add(
    new Binding("Rtf", bindingSource1, "rtfText", true));

When using a BindingNavigator connected to the BindingSource, you can easily navigate through the contained RTF documents:

RichTextBox compatible data binding with TX Text Control

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2017 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

Related Posts

Windows FormsGetting StartedTutorial

Windows Forms Tutorial: Create Your First Windows Forms C# Application

This tutorial shows how to create your first Windows Forms application with C# using TX Text Control .NET for Windows Forms in Visual Studio 2022.


ASP.NETASP.NET CoreMailMerge

How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#

Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…


AngularDocument EditorNode.js

Creating an Angular Document Editor Application with a Node.js WebSocket Server

This tutorial shows how to create an Angular application that uses the Document Editor with a Node.js WebSocket server.


ASP.NETASP.NET CoreMailMerge

Adding SVG Watermarks to Documents

This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.


ASP.NETASP.NET CoreMailMerge

Using MailMerge in ASP.NET Core 6 Web Applications

This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.