RichTextBox


The RichTextBox control is a text box that supports rich text extensions. Those extensions let the control display text that is bold, underlined, italicized, indented, in different fonts, and has other special visual properties.

The control can load and save its contents in plain-text files (in which case the formatting is lost) or in Rich Text Format (RTF) files (which preserve formatting).

A program can use the RichTextBox control’s Select method to select some of its text. It can then use one of the control’s properties to change the appearance of the selected text. For example, the following code selects the 10 characters starting with character 50 (the first character is number 0). It then sets the selection’s color to red and makes its font bold.

  rchNotes.Select(50, 10) rchNotes.SelectionColor = Color.Red rchNotes.SelectionFont = New Font(RichTextBox1.SelectionFont, FontStyle.Bold) 

The following table lists the RichTextBox control’s most useful properties.

Open table as spreadsheet

Property

Purpose

AcceptsTab

For multiline controls, determines whether pressing the Tab key adds a Tab to the text, rather than moving to the next control in the tab sequence.

AutoSize

For single-line controls, determines whether the control automatically sets its height for the fonts it contains.

BulletIndent

Determines the number of pixels added after a bullet as indentation. If you make the selection a bulleted paragraph and then change this value, the paragraph’s indentation is adjusted accordingly.

CanRedo

Indicates whether the control has any redo information that it can apply. See the discussion later in this section for an example.

CanUndo

Indicates whether the control has any undo information that it can apply. See the discussion later in this section for an example.

DetectUrls

Determines whether the control automatically recognizes web URLs when they are typed. If some text looks like a URL, the control displays it in blue, underlines it, and displays a hyperlink cursor (pointing hand) when the mouse hovers over the text. If the user clicks a recognized link, the control raises its LinkClicked event.

Lines

An array of strings giving the lines of text (separated by carriage returns) that are displayed by the control. You can use this property to give the control more than one paragraph at design time.

MaxLength

The maximum number of characters the user can enter into the control.

MultiLine

Determines whether the control displays multiple lines.

PreferredHeight

Returns the height a single-line control would want for the font size.

ReadOnly

Determines whether the user can modify the control’s text.

RedoActionName

The name of the action that will be redone if the program calls the control’s Redo method (for example, Typing or Delete). You can use this property to show the user what the next redo action is.

RightMargin

Determines the control’s right margin in pixels. The value 0 means there is no right margin.

Rtf

Determines the RTF codes for the control’s text. This includes the text itself, font information, and paragraph information (such as indentation, bulleting, and so forth).

ScrollBars

Determines which scroll bars the control displays. The values Horizontal, Vertical, and Both make the control display the corresponding scroll bars only when they are needed. The values ForcedHorizontal, ForcedVertical, and ForcedBoth make the control display the corresponding scroll bars always. The value None makes the control display no scroll bars. Note that some of these values may not always be honored. For example, if WordWrap is True or RightMargin is nonzero, the control never displays horizontal scroll bars.

SelectedRtf

Determines the selected text’s value and RTF formatting code.

SelectedText

Determines the selected text’s value without RTF formatting codes.

SelectionAlignment

Determines the selected text’s alignment (Left, Center, or Right).

SelectionBullet

Determines whether the selected text’s paragraph is bulleted.

SelectionCharOffset

Determines the selected text’s character offset above or below the baseline in pixels.

SelectionColor

Determines the selected text’s color.

SelectionFont

Determines the selected text’s font.

SelectionHangingIndent

Determines the selected text’s hanging indent.

SelectionIndent

Determines the number of pixels by which subsequent lines are indented in the selected text’s paragraph.

SelectionLength

Determines the length of the selected text. You can use Selection?Start and SelectionLength to select text, or you can use the Select method.

SelectionProtected

Determines whether the selected text is protected so that the user cannot modify it.

SelectionRightIndent

Determines the number of pixels by which the selected text’s paragraph is indented on the right.

SelectionStart

Determines the start of the selection. You can use SelectionStart and SelectionLength to select text, or you can use the Select method.

SelectionTabs

Determines the tabs for the selected text’s paragraph. For example, the array {20, 40, 60} sets tabs 20, 40, and 60 pixels from the left margin.

ShowSelectionMargin

If True, the control adds a selection margin on the left. If the user clicks inside this margin, the control selects the text to the right.

Text

Determines the control’s text, not including any formatting information. If you want to preserve formatting information, use the SelectedRtf property.

TextLength

Returns the length of the control’s text.

UndoActionName

The name of the action that will be undone if the program calls the control’s Undo method (for example, Typing or Delete). You can use this property to show the user what the next undo action is.

WordWrap

For multiline controls, determines whether the control wraps text to a new line if it is too long to fit.

The control also provides several important methods, as shown in the following table.

Open table as spreadsheet

Method

Purpose

AppendText

Adds text to the end of the control’s text.

CanPaste

Determines whether you can paste data of a specified format from the clipboard into the control.

Clear

Clears the control’s text.

ClearUndo

Empties the control’s undo list.

Copy

Copies the control’s selection to the clipboard.

Cut

Copies the control’s selection to the clipboard and removes it from the control’s text.

Find

Finds and selects text. Overloaded versions let you search for one of a group of characters or a string, possibly with options (MatchCase, NoHighlight, Reverse, or WholeWord), and possibly within a range of characters.

GetCharFromPosition

Finds the character closest to a specified (X, Y) position.

GetCharIndexFromPosition

Finds the index of the character closest to a specified (X, Y) position.

GetLineFromCharIndex

Returns the number of the line containing the specified character index.

GetPositionFromCharIndex

Returns the (X, Y) position of the character as a specified index.

LoadFile

Loads an RTF or text file or a stream into the control.

Paste

Pastes the clipboard’s contents into the control, replacing the current selection.

Redo

Reapplies the last action that was undone.

SaveFile

Saves the control’s text into an RTF or text file or stream.

ScrollToCaret

Scrolls the text so the insertion position is visible.

Select

Selects the indicated text.

SelectAll

Selects all of the control’s text.

Undo

Undoes the most recent action.

A program can use the CanUndo and CanRedo properties to determine when it should enable Undo and Redo buttons and menu items. The following code shows how a program can manage Undo and Redo buttons for the rchNotes control. When the control’s contents change, the TextChanged event handler enables or disables the buttons, depending on which information the control has. The buttons simply call the control’s Undo and Redo methods.

  Private Sub rchNotes_TextChanged(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles rchNotes.TextChanged     btnUndo.Enabled = rchNotes.CanUndo     btnRedo.Enabled = rchNotes.CanRedo End Sub Private Sub btnUndo_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnUndo.Click     rchNotes.Undo() End Sub Private Sub btnRedo_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnRedo.Click     rchNotes.Redo() End Sub 

The following version of the TextChanged event handler adds the values returned by the UndoActionName and RedoActionName methods to the buttons’ captions. For example, after the user deletes some text, the undo button’s caption says “Undo Delete.”

  Private Sub rchNotes_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles rchNotes.TextChanged     btnUndo.Enabled = rchNotes.CanUndo     btnRedo.Enabled = rchNotes.CanRedo     If btnUndo.Enabled Then         btnUndo.Text = "Undo " & rchNotes.UndoActionName     Else         btnUndo.Text = "Undo"     End If     If btnRedo.Enabled Then         If btnRedo.Enabled Then btnRedo.Text = "Redo " & rchNotes.RedoActionName     Else         If btnRedo.Enabled Then btnRedo.Text = "Redo"     End If End Sub 

The RichTextBox control’s most useful event is TextChanged. You can use this event to take action when the user changes the control’s text. For example, you can display a visible indication that the data has been modified or, as the previous examples show, you can enable and disable Undo and Redo buttons.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net