Lines, Scrolling and Zoom
Reads the lines of a RichTextBox, scrolls them into view, and sets the scroll bars, the margins and the zoom.
RichTextBox1.Text = "one" & vbCr & "two" & vbCr & "three"
Debug.Print RichTextBox1.GetLineCount
Debug.Print RichTextBox1.GetLine(2)
RichTextBox1.SelStart = 9 ' in "three"
Debug.Print RichTextBox1.GetLine(0) ' the line of the insertion point
Debug.Print RichTextBox1.GetLineFromChar(5)
' Output:
' 3
' two
' three
' 2
Lines
The methods that work on lines count them from 1, as those of a TextBoxW do, and a line is a line as the box shows it: a paragraph that the box wraps counts as several. GetLineCount returns the number of lines, GetLine the text of one, GetLineFromChar the line that a character is on, and CharFromPos the character at a point of the box.
Scrolling
ScrollToLine brings a line to the top of the box, and ScrollToCaret scrolls the insertion point into view. ScrollBars gives a multi-line box scroll bars; without them the text still scrolls as the insertion point moves. A scroll bar that is not needed is hidden, or shown disabled while DisableNoScroll is True.
The control raises Scroll each time its text scrolls, whether the user or the program scrolled it.
Zoom and margins
ZoomFactor draws the whole text larger or smaller than its format says, from a sixty-fourth of its size to 64 times it, without changing the format. 1 is the size of the format.
RichTextBox1.ZoomFactor = 1.5 ' the text half as large again
Debug.Print RichTextBox1.ZoomFactor
On Error Resume Next
RichTextBox1.ZoomFactor = 100
Debug.Print Err.Number; RichTextBox1.ZoomFactor
' Output:
' 1.5
' 380 1.5
LeftMargin and RightMargin are the empty strips at the sides of the text, and SelectionBar adds a wider strip at the left in which the user selects whole lines.
Properties
DisableNoScroll
Whether a scroll bar that is not needed is shown disabled rather than hidden. Boolean. Default: False.
It is set at design time: an assignment while the program runs raises run-time error 382.
LeftMargin, RightMargin
The widths of the empty strips at the left and at the right of the text, in the scale units of the control’s container. Single. Assigning -1 gives the margin the width that Windows works out from the font.
Any other negative value raises run-time error 380.
ScrollBars
The scroll bars of a multi-line box. A member of ScrollBarConstants: vbSBNone (the default), vbHorizontal, vbVertical or vbBoth. A box of one line has none.
Another value raises run-time error 380. A change makes the control’s window again, with the same text, the same selection and the same place of the scroll bars, so hWnd returns another handle afterwards.
SelectionBar
Whether the box has a strip at the left of the text in which the pointer is an arrow that points up and to the right, and a click selects the line beside it. Boolean. Default: False.
ZoomFactor
How much larger or smaller than its format the text is drawn. Double. Default: 1. A value outside the range from 1/64 to 64 raises run-time error 380, and the zoom stays as it was.
Methods
CharFromPos
Returns the place in the text, counted from 0, of the character nearest to a point.
Syntax: object.CharFromPos( X, Y ) As Long
- X, Y
- required The point, measured from the top left corner of the box in the scale units of the control’s container. Single.
GetLine
Returns the text of one line, without the carriage return that ends it.
Syntax: object.GetLine( LineNumber ) As String
- LineNumber
- required A Long: the number of the line, counted from 1, or 0 for the line of the insertion point.
A negative number, or a number greater than the number of lines, raises run-time error 380.
GetLineCount
Returns the number of lines in the box. A box that is empty has one line.
Syntax: object.GetLineCount As Long
GetLineFromChar
Returns the number of the line, counted from 1, that a character is on.
Syntax: object.GetLineFromChar( CharIndex ) As Long
- CharIndex
- required A Long: the place of the character in the text, counted from 0, or -1 for the line of the start of the selection.
A value less than -1 raises run-time error 380.
ScrollToCaret
Scrolls the box so that the insertion point shows, also while the box does not have the focus.
Syntax: object.ScrollToCaret
ScrollToLine
Scrolls the box so that a line is the first one that shows, where the text is long enough for that.
Syntax: object.ScrollToLine LineNumber
- LineNumber
- required A Long: the number of the line, counted from 1.
A negative number, or a number greater than the number of lines, raises run-time error 380.
Events
Scroll
Raised when the text of the box scrolls: the user moved a scroll bar or moved the insertion point out of view, or the program called ScrollToLine, moved the insertion point or assigned a text.
Syntax: object_Scroll( )
See Also
- RichTextBox control
- TextBoxW control – the same methods for the lines of a text box
- ScrollBarConstants – the values of ScrollBars