Skip to main content
Version: 4.2

RichTextBox

Namespace: Wisej.Web

Assembly: Wisej.Framework (4.2.0.0)

Edits formatted text with a toolbar, and reads and writes it as HTML, RTF or plain text.

public class RichTextBox : Control, IModified, IReadOnly

The content is kept as a sanitized HTML fragment: scripts, event attributes, unsafe addresses and unsupported elements are removed whenever it is assigned or edited in the browser. Rtf converts on the server, reading paragraphs, character formatting, colors, fonts, lists, tables, hyperlinks and PNG or JPEG pictures; other RTF features are dropped. Markdown reads and writes Markdown, and Text is the plain text. All of them can be read and assigned at any time. Edits made in the browser are sent with the next request and when the control loses the focus.

Example:

var editor = new RichTextBox { Dock = DockStyle.Fill };
editor.Rtf = File.ReadAllText("Letter.rtf");
editor.TextChanged += (sender, e) => saveButton.Enabled = editor.Modified;
this.Controls.Add(editor);

// later...
File.WriteAllText("Letter.rtf", editor.Rtf);
string html = editor.Html;

Constructors​

Instance member RichTextBox()​

Creates an empty editor with every toolbar button.

Properties​

Instance member AcceptsTab​

Boolean: Gets or sets whether Tab inserts a tab (and indents a list item) instead of moving the focus. (Default: False)

Shift+Tab outdents a list item; outside a list it still moves the focus to the previous control, so keyboard users can leave the editor. Browsers keep Ctrl+Tab to switch their tabs: the page never receives it.

Protected member DefaultSize​

Size: Gets the initial editor size.

Instance member Fonts​

String[]: Gets or sets the fonts offered by the font drop-down of the toolbar.

Null restores the default list. Names with quotes, semicolons, braces or angle brackets are left out. The browser shows a font only when it is installed on the user's computer or loaded by the page.

Instance member FontSizes​

Single[]: Gets or sets the font sizes, in points, offered by the font size drop-down of the toolbar.

Null restores the default list. Sizes are kept between 1 and 1,638 points, the sizes RTF can store.

Instance member Format​

RichTextBoxStreamType: Gets or sets the format of Text and of the content passed to Append and Insert. (Default: PlainText)

Changing it doesn't change the content, only how Text reads and writes it; it raises TextChanged so that bindings to Text read it again.

Throws:

Instance member Html​

String: Gets or sets the content as an HTML fragment. (Default: "")

The value is sanitized on assignment: only formatting elements, safe addresses and inline styles are kept. Reading it returns the sanitized fragment. Assigning it resets Modified.

Instance member Markdown​

String: Gets or sets the content as Markdown.

Reading it writes Markdown from the current content: headings, paragraphs, bold, italic, strikethrough, code, quotes, lists, links, pictures, rules and tables (GitHub tables). Colors, fonts, sizes, underline and alignment have no Markdown and are dropped. Assigning it reads CommonMark with GitHub tables and strikethrough, and resets Modified; HTML written in the Markdown is shown as text.

Instance member MaxLength​

Int32: Gets or sets the maximum number of characters the user can type or paste. (Default: 2147483647)

The limit applies to the plain text, as TextLength counts it, and to the user's edits only: content assigned from code can be longer. It is enforced by the browser; validate the length on the server when it matters.

Throws:

Instance member Modified​

Boolean: Gets or sets whether the content was edited in the browser since it was last assigned.

Instance member ReadOnly​

Boolean: Gets or sets whether the content can be edited in the browser. (Default: False)

A read-only editor hides nothing but disables its toolbar; hyperlinks raise LinkClicked on a click.

Instance member Rtf​

String: Gets or sets the content as an RTF document.

Reading it writes a new document from the current content. Assigning it converts the document to HTML and resets Modified; null or empty clears the content.

Throws:

Instance member SelectedHtml​

String: Gets or sets the selection in the browser as a sanitized HTML fragment, or replaces the selection with an HTML fragment.

Reading it returns the selection as of the last request from the browser; assigning it replaces the selection in the browser like InsertHtml.

Instance member SelectedText​

String: Gets or sets the plain text of the selection in the browser, or replaces the selection with plain text.

Reading it returns the selection as of the last request from the browser; assigning it replaces the selection in the browser like InsertText, and the edit reaches the server with the next request.

Instance member ShowToolbar​

Boolean: Gets or sets whether the formatting toolbar is shown. (Default: True)

Instance member Text​

String: Gets or sets the content in the format of Format: plain text, HTML, RTF or Markdown. (Default: "")

With the default PlainText, reading it returns one line for each paragraph, separated by "\n", and assigning it replaces the content with one unformatted paragraph for each line. The other formats read and write the content as Html, Rtf and Markdown do. Assigning it resets Modified.

Throws:

Instance member TextLength​

Int32: Gets the number of characters of the plain text.

Instance member ToolbarButtons​

RichTextBoxToolbarButtonCollection: Gets the buttons the application adds after the toolbar's own buttons.

A click raises ToolbarButtonClick. The buttons are shown also when ToolbarItems is None.

Instance member ToolbarItems​

RichTextBoxToolbarItems: Gets or sets the buttons of the formatting toolbar. (Default: All)

Instance member Watermark​

String: Gets or sets the text shown while the editor is empty. (Default: "")

Methods​

Instance member Append(content)​

Appends content in the format of Format.

ParameterTypeDescription
contentStringThe content to append.

Throws:

Instance member Append(content, format)​

Appends content in the given format: plain text as AppendText, the other formats as AppendHtml.

ParameterTypeDescription
contentStringThe content to append.
formatRichTextBoxStreamTypeThe format of the content.

Throws:

Instance member AppendHtml(html)​

Appends an HTML fragment; it is sanitized first.

ParameterTypeDescription
htmlStringThe fragment to append.

Instance member AppendText(text)​

Appends plain text as new paragraphs, one for each line.

ParameterTypeDescription
textStringThe text to append.

Instance member Clear()​

Removes the whole content.

Instance member ExecuteCommand(command, argument)​

Applies an editing command to the current selection in the browser, as the toolbar does.

ParameterTypeDescription
commandRichTextBoxCommandThe command.
argument optionalStringThe command's argument: a CSS color, an address, an HTML fragment, plain text or a block tag. See RichTextBoxCommand.

The edit reaches the server with the next request, as if the user had made it. Like the user, a command can't edit a read-only or disabled editor (SelectAll still works). The keyboard focus stays where it is, except for Undo and Redo, which focus the editor.

Example:

editor.ExecuteCommand(RichTextBoxCommand.InsertHtml, "<b>Signed:</b> " + userName);
editor.ExecuteCommand(RichTextBoxCommand.ForeColor, "#d91d34");

Instance member GetToolbarIcon(button)​

Returns the icon set for a button of the toolbar with SetToolbarIcon, or null when it has the theme's icon.

ParameterTypeDescription
buttonStringThe button, as for SetToolbarIcon.

Returns: String.

Instance member Insert(content)​

Replaces the selection in the browser with content in the format of Format.

ParameterTypeDescription
contentStringThe content to insert.

Throws:

Instance member Insert(content, format)​

Replaces the selection in the browser with content in the given format, like InsertText for plain text and InsertHtml for the other formats.

ParameterTypeDescription
contentStringThe content to insert.
formatRichTextBoxStreamTypeThe format of the content.

The edit reaches the server with the next request, as if the user had made it. Like the user, it can't edit a read-only or disabled editor: then nothing happens (Append works on the server and changes the content anyway). The keyboard focus stays where it is. Markdown or RTF of a single paragraph goes in the line at the caret, as a word pasted does.

Throws:

Instance member LoadFile(path, fileType)​

Replaces the content with a file.

ParameterTypeDescription
pathStringThe file to read.
fileType optionalRichTextBoxStreamTypeThe format of the file.

Instance member LoadFile(data, fileType)​

Replaces the content with the content of a stream, read to its end.

ParameterTypeDescription
dataStreamThe stream to read. It is not closed.
fileTypeRichTextBoxStreamTypeThe format of the content.

RTF is read byte by byte and decoded with its own code page and escapes; HTML, Markdown and plain text as UTF-8 unless they start with a byte order mark.

Protected member OnHandleCreated(e)​

Raises the HandleCreated event.

ParameterTypeDescription
eEventArgsA EventArgs that contains the event data.

Protected member OnHandleDestroyed(e)​

Raises the HandleDestroyed event.

ParameterTypeDescription
eEventArgsA EventArgs that contains the event data.

Protected member OnHtmlChanged(e)​

Raises HtmlChanged.

ParameterTypeDescription
eEventArgsEmpty event data.

Protected member OnLinkClicked(e)​

Raises LinkClicked.

ParameterTypeDescription
eRichTextBoxLinkClickedEventArgsThe activated address.

Protected member OnModifiedChanged(e)​

Raises ModifiedChanged.

ParameterTypeDescription
eEventArgsEmpty event data.

Protected member OnReadOnlyChanged(e)​

Raises ReadOnlyChanged.

ParameterTypeDescription
eEventArgsEmpty event data.

Protected member OnSelectionChanged(e)​

Raises SelectionChanged.

ParameterTypeDescription
eEventArgsEmpty event data.

Protected member OnToolbarButtonClick(e)​

Raises ToolbarButtonClick.

ParameterTypeDescription
eRichTextBoxToolbarButtonClickEventArgsThe clicked button.

Protected member OnWebEvent(e)​

Dispatches browser interactions to validated processors.

ParameterTypeDescription
eWisejEventArgsClient event data.

Protected member OnWebRender(config)​

Renders the sanitized content, the toolbar and the localized button captions.

ParameterTypeDescription
configObjectClient configuration.

Protected member OnWebUpdate(state)​

Applies the content edited in the browser.

ParameterTypeDescription
stateObjectClient state.

Instance member SaveFile(path, fileType)​

Writes the content to a file.

ParameterTypeDescription
pathStringThe file to write; it is replaced when it exists.
fileType optionalRichTextBoxStreamTypeThe format of the file.

Instance member SaveFile(data, fileType)​

Writes the content to a stream.

ParameterTypeDescription
dataStreamThe stream to write to. It is not closed.
fileTypeRichTextBoxStreamTypeThe format of the content.

RTF is written as ASCII; HTML, Markdown and plain text as UTF-8 without a byte order mark.

Instance member SelectAll()​

Selects the whole content in the browser.

Instance member SetToolbarIcon(button, imageSource)​

Replaces the icon of a button of the toolbar.

ParameterTypeDescription
buttonStringThe button: undo, redo, bold, italic, underline, strikethrough, superscript, subscript, forecolor, backcolor, bulletedlist, numberedlist, outdent, indent, alignleft, aligncenter, alignright, justify, link, unlink, table, picture or clearformatting.
imageSourceStringThe icon: a theme icon name, a URL or a file of the application; null or empty restores the theme's icon.

To change the icons of every editor, override the icon-richtextbox-* images in a theme mixin instead.

Throws:

Events​

Instance member HtmlChanged​

EventHandler Occurs when the value of the Html property has changed.

It is raised together with TextChanged, and lets data binding update the source of Html.

Instance member LinkClicked​

EventHandler<RichTextBoxLinkClickedEventArgs> Occurs when a hyperlink is clicked while the editor is read-only, or with Ctrl pressed while editing.

Without a handler the browser opens the address in a new tab. The event is raised only for the address of a hyperlink in the content, but the click comes from the browser: validate the address before the server uses it.

Instance member ModifiedChanged​

EventHandler Occurs when the value of the Modified property has changed.

Instance member ReadOnlyChanged​

EventHandler Occurs when the value of the ReadOnly property has changed.

Instance member SelectionChanged​

EventHandler Occurs when the selection in the browser changes.

The browser sends the selection only while there is a handler, a short time after the user stops moving it. SelectedText and SelectedHtml are up to date in the handler.

Instance member ToolbarButtonClick​

EventHandler<RichTextBoxToolbarButtonClickEventArgs> Occurs when a button of ToolbarButtons is clicked.

The edits made in the browser before the click are already in Html and the selection in SelectedHtml.

Implements​

NameDescription
IUserDataProvides access to the UserData and Tag properties associated to the component implementing this interface.
IBindableComponentBindable components implement this interface.
IDropTargetControls that support drag & drop operations implement this interface.
IReadOnlyProvides access to the ReadOnly property for coontrols that support the read-only mode.
IModifiedProvides access to the Modified property and ModifiedChanged event for controls that implement this interface.
IWisejComponentAll wisej components implement this interface.
IWisejControlAll wisej controls derived from the Control class must implement this interface.
IWisejSerializableAllows an object to serialize itself.