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.
- C#
- VB.NET
public class RichTextBox : Control, IModified, IReadOnly
Public Class RichTextBox
Inherits Control
Implements 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
RichTextBox()
Creates an empty editor with every toolbar button.
Properties
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.
DefaultSize
Size: Gets the initial editor size.
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.
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.
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:
- InvalidEnumArgumentException The value is not one of the RichTextBoxStreamType values.
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.
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.
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:
- ArgumentOutOfRangeException The value is negative.
Modified
Boolean: Gets or sets whether the content was edited in the browser since it was last assigned.
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.
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:
- ArgumentException The value is not an RTF document.
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.
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.
ShowToolbar
Boolean: Gets or sets whether the formatting toolbar is shown. (Default: True)
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:
- ArgumentException Format is RTF and the value is not an RTF document.
TextLength
Int32: Gets the number of characters of the plain text.
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.
ToolbarItems
RichTextBoxToolbarItems: Gets or sets the buttons of the formatting toolbar. (Default: All)
Watermark
String: Gets or sets the text shown while the editor is empty. (Default: "")
Methods
Append(content)
Appends content in the format of Format.
| Parameter | Type | Description |
|---|---|---|
| content | String | The content to append. |
Throws:
- ArgumentException Format is RTF and the content is not an RTF document.
Append(content, format)
Appends content in the given format: plain text as AppendText, the other formats as AppendHtml.
| Parameter | Type | Description |
|---|---|---|
| content | String | The content to append. |
| format | RichTextBoxStreamType | The format of the content. |
Throws:
- ArgumentException The format is RTF and the content is not an RTF document.
AppendHtml(html)
Appends an HTML fragment; it is sanitized first.
| Parameter | Type | Description |
|---|---|---|
| html | String | The fragment to append. |
AppendText(text)
Appends plain text as new paragraphs, one for each line.
| Parameter | Type | Description |
|---|---|---|
| text | String | The text to append. |
Clear()
Removes the whole content.
ExecuteCommand(command, argument)
Applies an editing command to the current selection in the browser, as the toolbar does.
| Parameter | Type | Description |
|---|---|---|
| command | RichTextBoxCommand | The command. |
| argument | String | The 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");
GetToolbarIcon(button)
Returns the icon set for a button of the toolbar with SetToolbarIcon, or null when it has the theme's icon.
| Parameter | Type | Description |
|---|---|---|
| button | String | The button, as for SetToolbarIcon. |
Returns: String.
Insert(content)
Replaces the selection in the browser with content in the format of Format.
| Parameter | Type | Description |
|---|---|---|
| content | String | The content to insert. |
Throws:
- ArgumentException Format is RTF and the content is not an RTF document.
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.
| Parameter | Type | Description |
|---|---|---|
| content | String | The content to insert. |
| format | RichTextBoxStreamType | The 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:
- ArgumentException The format is RTF and the content is not an RTF document.
LoadFile(path, fileType)
Replaces the content with a file.
| Parameter | Type | Description |
|---|---|---|
| path | String | The file to read. |
| fileType | RichTextBoxStreamType | The format of the file. |
LoadFile(data, fileType)
Replaces the content with the content of a stream, read to its end.
| Parameter | Type | Description |
|---|---|---|
| data | Stream | The stream to read. It is not closed. |
| fileType | RichTextBoxStreamType | The 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.
OnHandleCreated(e)
Raises the HandleCreated event.
OnHandleDestroyed(e)
Raises the HandleDestroyed event.
OnHtmlChanged(e)
Raises HtmlChanged.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs | Empty event data. |
OnLinkClicked(e)
Raises LinkClicked.
| Parameter | Type | Description |
|---|---|---|
| e | RichTextBoxLinkClickedEventArgs | The activated address. |
OnModifiedChanged(e)
Raises ModifiedChanged.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs | Empty event data. |
OnReadOnlyChanged(e)
Raises ReadOnlyChanged.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs | Empty event data. |
OnSelectionChanged(e)
Raises SelectionChanged.
| Parameter | Type | Description |
|---|---|---|
| e | EventArgs | Empty event data. |
OnToolbarButtonClick(e)
Raises ToolbarButtonClick.
| Parameter | Type | Description |
|---|---|---|
| e | RichTextBoxToolbarButtonClickEventArgs | The clicked button. |
OnWebEvent(e)
Dispatches browser interactions to validated processors.
| Parameter | Type | Description |
|---|---|---|
| e | WisejEventArgs | Client event data. |
OnWebRender(config)
Renders the sanitized content, the toolbar and the localized button captions.
| Parameter | Type | Description |
|---|---|---|
| config | Object | Client configuration. |
OnWebUpdate(state)
Applies the content edited in the browser.
| Parameter | Type | Description |
|---|---|---|
| state | Object | Client state. |
SaveFile(path, fileType)
Writes the content to a file.
| Parameter | Type | Description |
|---|---|---|
| path | String | The file to write; it is replaced when it exists. |
| fileType | RichTextBoxStreamType | The format of the file. |
SaveFile(data, fileType)
Writes the content to a stream.
| Parameter | Type | Description |
|---|---|---|
| data | Stream | The stream to write to. It is not closed. |
| fileType | RichTextBoxStreamType | The format of the content. |
RTF is written as ASCII; HTML, Markdown and plain text as UTF-8 without a byte order mark.
SelectAll()
Selects the whole content in the browser.
SetToolbarIcon(button, imageSource)
Replaces the icon of a button of the toolbar.
| Parameter | Type | Description |
|---|---|---|
| button | String | The 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. |
| imageSource | String | The 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:
- ArgumentException The button is not one of the toolbar.
Events
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.
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.
ModifiedChanged
EventHandler Occurs when the value of the Modified property has changed.
ReadOnlyChanged
EventHandler Occurs when the value of the ReadOnly property has changed.
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.
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
| Name | Description |
|---|---|
| IUserData | Provides access to the UserData and Tag properties associated to the component implementing this interface. |
| IBindableComponent | Bindable components implement this interface. |
| IDropTarget | Controls that support drag & drop operations implement this interface. |
| IReadOnly | Provides access to the ReadOnly property for coontrols that support the read-only mode. |
| IModified | Provides access to the Modified property and ModifiedChanged event for controls that implement this interface. |
| IWisejComponent | All wisej components implement this interface. |
| IWisejControl | All wisej controls derived from the Control class must implement this interface. |
| IWisejSerializable | Allows an object to serialize itself. |