Condividi tramite


Procedura: impostare punti di tabulazione nel testo disegnato

Aggiornamento: novembre 2007

È possibile impostare i punti di tabulazione nel testo chiamando il metodo SetTabStops di un oggetto StringFormat e quindi passando l'oggetto StringFormat al metodo DrawString della classe Graphics.

Nota:

L'oggetto System.Windows.Forms.TextRenderer non supporta l'aggiunta di punti di tabulazione al testo disegnato, sebbene sia possibile espandere i punti di tabulazione esistenti utilizzando il flag TextFormatFlags.ExpandTabs.

Esempio

Nell'esempio riportato di seguito vengono impostati i punti di tabulazione sui valori 150, 250 e 350, quindi viene visualizzato un elenco tabulato di nomi e risultati di esami.

Nell'illustrazione che segue si mostra il testo tabulato.

Testo caratteri

Nel codice seguente vengono passati due argomenti al metodo SetTabStops. Il secondo argomento è una matrice che contiene offset di tabulazione. Il primo argomento passato a SetTabStops è 0, che indica che il primo offset nella matrice viene misurato dalla posizione 0, ovvero il margine sinistro del rettangolo di delimitazione.

Dim myText As String = _
   "Name" & ControlChars.Tab & _
   "Test 1" & ControlChars.Tab & _
   "Test 2" & ControlChars.Tab & _
   "Test 3" & ControlChars.Cr

myText = myText & "Joe" & ControlChars.Tab & _
                  "95" & ControlChars.Tab & _
                  "88" & ControlChars.Tab & _
                  "91" & ControlChars.Cr
myText = myText & "Mary" & ControlChars.Tab & _
                  "98" & ControlChars.Tab & _
                  "84" & ControlChars.Tab & _
                  "90" & ControlChars.Cr
myText = myText & "Sam" & ControlChars.Tab & _
                  "42" & ControlChars.Tab & _
                  "76" & ControlChars.Tab & _
                  "98" & ControlChars.Cr
myText = myText & "Jane" & ControlChars.Tab & _
                  "65" & ControlChars.Tab & _
                  "73" & ControlChars.Tab & _
                  "92" & ControlChars.Cr

Dim fontFamily As New FontFamily("Courier New")
Dim font As New Font( _
   fontFamily, _
   12, _
   FontStyle.Regular, _
   GraphicsUnit.Point)
Dim rect As New Rectangle(10, 10, 450, 100)
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim tabs As Single() = {150, 100, 100, 100}

stringFormat.SetTabStops(0, tabs)

e.Graphics.DrawString(myText, font, solidBrush, RectangleF.op_implicit(rect), stringFormat)

Dim pen As Pen = Pens.Black
e.Graphics.DrawRectangle(pen, rect)

string text = "Name\tTest 1\tTest 2\tTest 3\n";
text = text + "Joe\t95\t88\t91\n";
text = text + "Mary\t98\t84\t90\n";
text = text + "Sam\t42\t76\t98\n";
text = text + "Jane\t65\t73\t92\n";

FontFamily fontFamily = new FontFamily("Courier New");
Font font = new Font(
   fontFamily,
   12,
   FontStyle.Regular,
   GraphicsUnit.Point);
Rectangle rect = new Rectangle(10, 10, 450, 100);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
float[] tabs = { 150, 100, 100, 100 };

stringFormat.SetTabStops(0, tabs);

e.Graphics.DrawString(text, font, solidBrush, rect, stringFormat);

Pen pen = Pens.Black;
e.Graphics.DrawRectangle(pen, rect);

Compilazione del codice

Vedere anche

Attività

Procedura: creare testo con GDI

Altre risorse

Utilizzo di tipi di carattere e testo