Skip to main content

RichTextBox

Wisej.NET 4.2.0+

A RichTextBox with the full formatting toolbar, one application button, and a delivery letter being written.

The RichTextBox is a formatted text editor built into Wisej.NET: a toolbar for headings, fonts, colors, lists, alignment, links, tables and pictures above an editing area, with no third-party script to license or load. Use it where users write something that needs more than a TextBox can hold: order notes, letters, ticket replies, product descriptions.

The HTML editors in the extensions (CKEditor, TinyMCE, QuillJS) wrap complete third-party editors and expose everything they do. The RichTextBox is smaller and owned by the framework, and it does two things those wrappers leave to you: it sanitizes every piece of content it receives, and it converts that content on the server between HTML, RTF, Markdown and plain text. Code ported from the Windows Forms RichTextBox will find the familiar Rtf, SelectedText, Modified, LoadFile and SaveFile members.

info

For a complete list of properties, methods, and events, see the API documentation.

Features​

One Content, Four Formats​

The content is held as a sanitized HTML fragment, and four properties read and write it:

PropertyReadsAssigns
HtmlThe sanitized fragment.Sanitizes the fragment and keeps only formatting elements, safe addresses and inline styles.
RtfA new RTF document written from the content.Converts the document: paragraphs, character formatting, colors, fonts, lists, tables, hyperlinks, and PNG or JPEG pictures. Other RTF features are dropped.
MarkdownHeadings, paragraphs, bold, italic, strikethrough, code, quotes, lists, links, pictures, rules and GitHub tables. Colors, fonts, sizes, underline and alignment have no Markdown and are dropped.Reads CommonMark with GitHub tables, strikethrough and task lists. HTML written in the Markdown is shown as text.
TextDepends on Format; by default the plain text, one line per paragraph.Depends on Format; by default one unformatted paragraph per line.

All of them can be read and assigned at any time, and the conversions run on the server, so a document loaded as RTF can be saved as Markdown without a round trip to the browser. Format, of type RichTextBoxStreamType (RichText, PlainText, Html or Markdown), decides what Text and the single-argument Append and Insert methods speak. It changes how the content is read, never the content itself.

Sanitized by Design​

Scripts, event attributes, unsafe addresses and unsupported elements are removed whenever content is assigned from code and whenever it comes back from the browser. Markdown never passes raw HTML through. That makes it safe to store what users type and to show it again, in this editor or in an HtmlPanel. Two things are still yours to check: MaxLength is enforced by the browser, so validate the length on the server when it matters; and a LinkClicked address comes from the browser, so validate it before the server uses it.

The Toolbar​

ToolbarItems is a flags enum of RichTextBoxToolbarItems that composes the toolbar: History, BlockFormat, FontName, FontSize, Bold, Italic, Underline, Strikethrough, Script, ForeColor, BackColor, Lists, Indent, Alignment, Link, Table, Picture and ClearFormatting, or All. The buttons always appear in a fixed order, grouped by kind. Fonts and FontSizes set the entries of the two drop-downs, and ShowToolbar hides the toolbar altogether.

ToolbarButtons appends buttons of your own after the built-in ones; a click raises ToolbarButtonClick. SetToolbarIcon replaces the icon of a built-in button on one editor; to change it on every editor, override the icon-richtextbox-* images in a theme mixin instead.

Editing from Code​

ExecuteCommand applies the same commands as the toolbar to the selection in the browser, from Bold and ForeColor to InsertHtml and InsertTable. SelectedText and SelectedHtml read the selection as of the last request and, when assigned, replace it. Insert replaces the selection with content in any format, while Append, AppendText and AppendHtml add to the end of the content on the server. Clear and SelectAll do what they say.

Edits made in the browser reach the server with the next request and when the editor loses the focus. Modified turns true when the user edits the content and is reset whenever the content is assigned.

With ReadOnly set to true the toolbar is disabled and a click on a hyperlink raises LinkClicked; while editing, it takes Ctrl+click. Without a handler the browser opens the address in a new tab.

How To​

Load and Save Documents​

// Open an RTF letter from the customer's folder.
this.richTextBox1.LoadFile(Path.Combine(folder, "Letter.rtf"), RichTextBoxStreamType.RichText);

// Save it back as RTF, and keep a Markdown copy for the CRM.
this.richTextBox1.SaveFile(Path.Combine(folder, "Letter.rtf"));
File.WriteAllText(Path.Combine(folder, "Letter.md"), this.richTextBox1.Markdown);

// Or work with streams, for example a blob from the database.
using (var stream = new MemoryStream(order.NotesRtf))
this.richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);

Bind the Notes of an Order​

HtmlChanged is raised together with TextChanged, so Html can be data bound like Text.

this.richTextBox1.DataBindings.Add("Html", this.orderBindingSource, "NotesHtml");

this.richTextBox1.ModifiedChanged += (s, e) =>
this.saveButton.Enabled = this.richTextBox1.Modified;

Trim the Toolbar and Add a Button​

var editor = new RichTextBox
{
Dock = DockStyle.Fill,
Watermark = "Write your reply to the customer…",
ToolbarItems = RichTextBoxToolbarItems.History | RichTextBoxToolbarItems.Bold |
RichTextBoxToolbarItems.Italic | RichTextBoxToolbarItems.Lists | RichTextBoxToolbarItems.Link,
MaxLength = 4000
};
editor.ToolbarButtons.Add(new RichTextBoxToolbarButton("signature", "Images/signature.svg", "Insert signature"));

editor.ToolbarButtonClick += (s, e) =>
{
if (e.Button.Name == "signature")
editor.ExecuteCommand(RichTextBoxCommand.InsertHtml, "<p><b>" + agent.Name + "</b><br>Customer Service</p>");
};
this.Controls.Add(editor);
this.richTextBox1.ReadOnly = true;
this.richTextBox1.LinkClicked += (s, e) =>
{
if (e.LinkText.StartsWith("https://tracking.example.com/"))
Application.Navigate(e.LinkText, "_blank");
};

Advanced​

JavaScript Widget​

ItemDescription
Class name"wisej.web.RichTextBox"
Theme appearance"richtextbox", with the "toolbar", "button", "blockformat", "separator", "editor", "placeholder", "palette", "swatch", "automatic", "linkeditor" and "apply" components. See Themes.