This documentation is archived and is not being maintained.

CharacterRange Structure

Specifies a range of character positions within a string.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

'Declaration
Public Structure CharacterRange
'Usage
Dim instance As CharacterRange

The following code example demonstrates how to create a CharacterRange and use it to highlight part of a string. This example is designed to be used with Windows Forms. Paste the example into a form and call the HighlightACharacterRange method when handling the form's Paint event, passing e as PaintEventArgs.

Private Sub HighlightACharacterRange(ByVal e As PaintEventArgs)

    ' Declare the string to draw. 
    Dim message As String = _
        "This is the string to highlight a word in." 

    ' Declare the word to highlight. 
    Dim searchWord As String = "string" 

    ' Create a CharacterRange array with the searchWord  
    ' location and length. 
    Dim ranges() As CharacterRange = _
        New CharacterRange() _
            {New CharacterRange(message.IndexOf(searchWord), _
            searchWord.Length)}

    ' Construct a StringFormat object. 
    Dim stringFormat1 As New StringFormat

    ' Set the ranges on the StringFormat object.
    stringFormat1.SetMeasurableCharacterRanges(ranges)

    ' Declare the font to write the message in. 
    Dim largeFont As Font = New Font(FontFamily.GenericSansSerif, _
        16.0F, GraphicsUnit.Pixel)

    ' Construct a new Rectangle. 
    Dim displayRectangle As New Rectangle(20, 20, 200, 100)

    ' Convert the Rectangle to a RectangleF. 
    Dim displayRectangleF As RectangleF = _
        RectangleF.op_Implicit(displayRectangle)

    ' Get the Region to highlight by calling the  
    ' MeasureCharacterRanges method. 
    Dim charRegion() As Region = _
        e.Graphics.MeasureCharacterRanges(message, _
        largeFont, displayRectangleF, stringFormat1)

    ' Draw the message string on the form.
    e.Graphics.DrawString(message, largeFont, Brushes.Blue, _
        displayRectangleF)

    ' Fill in the region using a semi-transparent color.
    e.Graphics.FillRegion(New SolidBrush(Color.FromArgb(50, _
        Color.Fuchsia)), charRegion(0))

End Sub

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Show: