This Control, derived from
TextBoxBase, extends the basic text box
functionality to include character and paragraph formatting. It can
also display embedded objects such as images, all with an arbitrary
ZoomFactor and optional
ScrollBars.
Content can be assigned with the standard Text
property and AppendText() methods. In addition to
these TextBoxBase methods, LoadFile() will bring plain ASCII text or an RTF file into the
control. Alternatively, you can assign formatted
Rtf markup directly. If the content contains
embedded objects, the string obtained from the
Text property will include a placeholder character
for each such item. The content can be saved to a file as either
ASCII or RTF markup using the SaveFile() method.
Once you have some text in the control, you can begin to manipulate
it. First, some of this content must be selected, either by the user,
or programmatically. To assist the user, you can enable the
AutoWordSelection property. This provides the
highlight-completion behavior seen in Microsoft WordPad: if you
double-click or select a portion of a word, the entire word will be
highlighted. You can get and set the
SelectionStart and the
SelectionLength independently, or set both at once
with the Select() or SelectAll() methods. If the SelectionLength is 0,
the SelectionStart represents the position of the
caret. ScrollToCaret() ensures that the current
cursor position is visible on screen.
You can also get the SelectionType. This returns a
combination of flags from the
RichTextBoxSelectionTypes enumeration. For
example, you can determine whether the selection is
Empty, Text only, or contains
an embedded Object. Bind to the
SelectionChanged event to be notified when the
selection is modified. You can get or set a string object
representing either the SelectedText or
SelectedRtf (analogous to Text
and Rtf).
You can then set the SelectionAlignment, a
SelectionBullet style (and the
BulletIndent), SelectionIndent,
SelectionHangingIndent,
SelectionRightIndent, an array of
SelectionTabs, the
SelectionColor, and
SelectionFont.
Text or objects from the clipboard or drag-and-drop operations can be
inserted at the current caret position using the Paste() method. To determine whether a particular
object's DataFormats.Format is
suitable, you should call CanPaste(). The
Cut( ) and Copy() methods will
place the current selection onto the clipboard (removing the
selection or leaving it in place respectively).
You can Find() text within the control,
optionally specifying one or more of the
RichTextBoxFinds flags to determine what the
control does when it finds the specified string.
It also supports multilevel undo and redo. There are properties to
retrieve the current UndoActionName and
RedoActionName. If either of these is the empty
string (""), it
indicates that no action is available at this time. Undo() and Redo() methods invoke the current
action, while ClearUndo() empties the undo list.
One final feature the control offers is the ability to automatically
DetectUrls in the body of the text. It reformats
the text for you and raises the LinkClicked events
when the user clicks them.
public class RichTextBox : TextBoxBase {
// Public Constructors
public RichTextBox();
// Public Instance Properties
public override bool AllowDrop{set; get; }
// overrides Control
public override bool AutoSize{set; get; }
// overrides TextBoxBase
public bool AutoWordSelection{set; get; }
public override Image BackgroundImage{set; get; }
// overrides TextBoxBase
public int BulletIndent{set; get; }
public bool CanRedo{get; }
public bool DetectUrls{set; get; }
public override Font Font{set; get; }
// overrides Control
public override Color ForeColor{set; get; }
// overrides TextBoxBase
public override int MaxLength{set; get; }
// overrides TextBoxBase
public override bool Multiline{set; get; }
// overrides TextBoxBase
public string RedoActionName{get; }
public int RightMargin{set; get; }
public string Rtf{set; get; }
public RichTextBoxScrollBars ScrollBars{set; get; }
public string SelectedRtf{set; get; }
public override string SelectedText{set; get; }
// overrides TextBoxBase
public HorizontalAlignment SelectionAlignment{set; get; }
public bool SelectionBullet{set; get; }
public int SelectionCharOffset{set; get; }
public Color SelectionColor{set; get; }
public Font SelectionFont{set; get; }
public int SelectionHangingIndent{set; get; }
public int SelectionIndent{set; get; }
public override int SelectionLength{set; get; }
// overrides TextBoxBase
public bool SelectionProtected{set; get; }
public int SelectionRightIndent{set; get; }
public int[ ] SelectionTabs{set; get; }
public RichTextBoxSelectionTypes SelectionType{get; }
public bool ShowSelectionMargin{set; get; }
public override string Text{set; get; }
// overrides TextBoxBase
public override int TextLength{get; }
// overrides TextBoxBase
public string UndoActionName{get; }
public float ZoomFactor{set; get; }
// Protected Instance Properties
protected override CreateParams CreateParams{get; }
// overrides TextBoxBase
protected override Size DefaultSize{get; }
// overrides TextBoxBase
// Public Instance Methods
public bool CanPaste(Format clipFormat);
public int Find(char[ ] characterSet);
public int Find(char[ ] characterSet, int start);
public int Find(char[ ] characterSet, int start, int end);
public int Find(string str);
public int Find(string str, int start, int end, RichTextBoxFinds options);
public int Find(string str, int start, RichTextBoxFinds options);
public int Find(string str, RichTextBoxFinds options);
public char GetCharFromPosition(System.Drawing.Point pt);
public int GetCharIndexFromPosition(System.Drawing.Point pt);
public int GetLineFromCharIndex(int index);
public Point GetPositionFromCharIndex(int index);
public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType);
public void LoadFile(string path);
public void LoadFile(string path, RichTextBoxStreamType fileType);
public void Paste(Format clipFormat);
public void Redo();
public void SaveFile(System.IO.Stream data, RichTextBoxStreamType fileType);
public void SaveFile(string path);
public void SaveFile(string path, RichTextBoxStreamType fileType);
// Protected Instance Methods
protected virtual object CreateRichEditOleCallback();
protected override void OnBackColorChanged(EventArgs e); // overrides Control
protected virtual void OnContentsResized(ContentsResizedEventArgs e);
protected override void OnContextMenuChanged(EventArgs e); // overrides Control
protected override void OnHandleCreated(EventArgs e); // overrides TextBoxBase
protected override void OnHandleDestroyed(EventArgs e); // overrides TextBoxBase
protected virtual void OnHScroll(EventArgs e);
protected virtual void OnImeChange(EventArgs e);
protected virtual void OnLinkClicked(LinkClickedEventArgs e);
protected virtual void OnProtected(EventArgs e);
protected override void OnRightToLeftChanged(EventArgs e); // overrides Control
protected virtual void OnSelectionChanged(EventArgs e);
protected override void OnSystemColorsChanged(EventArgs e); // overrides Control
protected override void OnTextChanged(EventArgs e); // overrides Control
protected virtual void OnVScroll(EventArgs e);
protected override void WndProc(ref Message m); // overrides TextBoxBase
// Events
public event ContentsResizedEventHandler ContentsResized;
public event EventHandler DoubleClick;
// overrides Control
public event DragEventHandler DragDrop;
// overrides Control
public event DragEventHandler DragEnter;
// overrides Control
public event EventHandler DragLeave;
// overrides Control
public event DragEventHandler DragOver;
// overrides Control
public event GiveFeedbackEventHandler GiveFeedback;
// overrides Control
public event EventHandler HScroll;
public event EventHandler ImeChange;
public event LinkClickedEventHandler LinkClicked;
public event EventHandler Protected;
public event QueryContinueDragEventHandler QueryContinueDrag;
// overrides Control
public event EventHandler SelectionChanged;
public event EventHandler VScroll;
}