Export (0) Print
Expand All

CharacterRange Structure

Specifies a range of character positions within a string.

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

public struct CharacterRange
public final class CharacterRange extends ValueType
Not applicable.

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 void HighlightACharacterRange(PaintEventArgs e)
{

    // Declare the string to draw.
    string message = "This is the string to highlight a word in.";

    // Declare the word to highlight.
    string searchWord = "string";

    // Create a CharacterRange array with the searchWord 
    // location and length.
    CharacterRange[] ranges = 
        new CharacterRange[]{new CharacterRange
        (message.IndexOf(searchWord), searchWord.Length)};

    // Construct a StringFormat object.
    StringFormat stringFormat1 = new StringFormat();

    // Set the ranges on the StringFormat object.
    stringFormat1.SetMeasurableCharacterRanges(ranges);

    // Declare the font to write the message in.
    Font largeFont = new Font(FontFamily.GenericSansSerif, 16.0F,
        GraphicsUnit.Pixel);

    // Construct a new Rectangle.
    Rectangle displayRectangle = new Rectangle(20, 20, 200, 100);

    // Convert the Rectangle to a RectangleF.
    RectangleF displayRectangleF = (RectangleF)displayRectangle;

    // Get the Region to highlight by calling the 
    // MeasureCharacterRanges method.
    Region[] charRegion = 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]);

    largeFont.Dispose();

}

private void HighlightACharacterRange(PaintEventArgs e)
{
    // Declare the string to draw.
    String message = "This is the string to highlight a word in.";
    // Declare the word to highlight.
    String searchWord = "string";
    // Create a CharacterRange array with the searchWord 
    // location and length.
    CharacterRange ranges[] = new CharacterRange[] 
        { new CharacterRange(message.IndexOf(searchWord), 
        searchWord.get_Length()) };
    // Construct a StringFormat object.
    StringFormat stringFormat1 = new StringFormat();
    // Set the ranges on the StringFormat object.
    stringFormat1.SetMeasurableCharacterRanges(ranges);
    // Declare the font to write the message in.
    Font largeFont = new Font(FontFamily.get_GenericSansSerif(), 
        16, GraphicsUnit.Pixel);
    // Construct a new Rectangle.
    Rectangle displayRectangle = new Rectangle(20, 20, 200, 100);
    // Convert the Rectangle to a RectangleF.
    RectangleF displayRectangleF = RectangleF.op_Implicit(displayRectangle);
    // Get the Region to highlight by calling the 
    // MeasureCharacterRanges method.
    Region charRegion[] = e.get_Graphics().MeasureCharacterRanges(message, 
        largeFont, displayRectangleF, stringFormat1);
    // Draw the message string on the form.
    e.get_Graphics().DrawString(message, largeFont, Brushes.get_Blue(),
        displayRectangleF);
    // Fill in the region using a semi-transparent color.
    e.get_Graphics().FillRegion(new SolidBrush(Color.FromArgb(50, 
        Color.get_Fuchsia())), charRegion[0]);
    largeFont.Dispose();
} //HighlightACharacterRange

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 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft