CharacterRange type

A range of characters in a string: the position of the first character and the number of characters.

A CharacterRange names the part of a string whose layout is wanted. A StringFormat holds up to 32 of them (MeasurableCharacterRanges), and Graphics.MeasureCharacterRanges returns the space that each one takes when the string is drawn. The type mirrors the GDI+ CharacterRange structure.

Fields

Field Type Meaning
First Long The position of the first character of the range, counted from 0.
Length Long The number of characters in the range.

Creating

Syntax: range = CharacterRange( )

Syntax: range = CharacterRange( first, length )

first
required A Long for First.
length
required A Long for Length.

CharacterRange( ) returns a range with both fields 0. A variable declared As CharacterRange starts the same way, so the function is a convenience for setting both fields in one expression.

Dim user As GdiPlusUser = GdiPlusUser()

Dim r As CharacterRange = CharacterRange(4, 3)
Debug.Print r.First & " " & r.Length    ' 4 3

Dim zero As CharacterRange = CharacterRange()
Debug.Print zero.First & " " & zero.Length    ' 0 0

' The word "world" in "Hello world":
Dim text As String = "Hello world"
Dim word As CharacterRange = CharacterRange(InStr(text, "world") - 1, Len("world"))
Debug.Print Mid$(text, word.First + 1, word.Length)    ' world

See Also