Leggere in inglese

Condividi tramite


CharacterRange Struct

Definizione

Specifica un intervallo di posizioni di caratteri all'interno di una stringa.

C#
public struct CharacterRange
C#
public struct CharacterRange : IEquatable<System.Drawing.CharacterRange>
Ereditarietà
CharacterRange
Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato come creare un oggetto CharacterRange e usarlo per evidenziare parte di una stringa. Questo esempio è progettato per essere usato con Windows Forms. Incollare l'esempio in un modulo e chiamare il metodo durante la HighlightACharacterRange gestione dell'evento del Paint modulo, passando e come 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();
}

Costruttori

CharacterRange(Int32, Int32)

Inizializza una nuova istanza della struttura CharacterRange, specificando un intervallo di posizioni di caratteri all'interno di una stringa.

Proprietà

First

Ottiene o imposta la posizione nella stringa del primo carattere di questo oggetto CharacterRange.

Length

Ottiene o imposta il numero di posizioni in questo CharacterRange.

Metodi

Equals(CharacterRange)

Indica se l'istanza corrente è uguale a un'altra istanza dello stesso tipo.

Equals(Object)

Ottiene un valore che indica se l'oggetto è equivalente all'oggetto specificato.

GetHashCode()

Restituisce il codice hash per l'istanza.

Operatori

Equality(CharacterRange, CharacterRange)

Confronta due oggetti CharacterRange. Ottiene un valore che indica se i valori First e Length dei due oggetti CharacterRange sono uguali.

Inequality(CharacterRange, CharacterRange)

Confronta due oggetti CharacterRange. Ottiene un valore che indica se i valori First o Length dei due oggetti CharacterRange non sono uguali.

Si applica a

Prodotto Versioni
.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, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9