Edit

Share via


CharacterRange Struct

Definition

Specifies a range of character positions within a string.

C#
public struct CharacterRange : IEquatable<System.Drawing.CharacterRange>
C#
public struct CharacterRange
Inheritance
CharacterRange
Implements

Examples

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.

C#
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();
}

Constructors

CharacterRange(Int32, Int32)

Initializes a new instance of the CharacterRange structure, specifying a range of character positions within a string.

Properties

First

Gets or sets the position in the string of the first character of this CharacterRange.

Length

Gets or sets the number of positions in this CharacterRange.

Methods

Equals(CharacterRange)

Indicates whether the current instance is equal to another instance of the same type.

Equals(Object)

Gets a value indicating whether this object is equivalent to the specified object.

GetHashCode()

Returns the hash code for this instance.

Operators

Equality(CharacterRange, CharacterRange)

Compares two CharacterRange objects. Gets a value indicating whether the First and Length values of the two CharacterRange objects are equal.

Inequality(CharacterRange, CharacterRange)

Compares two CharacterRange objects. Gets a value indicating whether the First or Length values of the two CharacterRange objects are not equal.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9