Export (0) Print
Expand All

Formatting Text

To apply special formatting to text, initialize a StringFormat object and pass the address of that object to the DrawString method of the Graphics class.

To draw formatted text in a rectangle, you need Graphics, FontFamily, Font, RectF, StringFormat, and Brush objects.

Aligning Text

The following example draws text in a rectangle. Each line of text is centered, and the entire block of text is centered (top to bottom) in the rectangle.

Dim myText As String = "Use StringFormat and RectangleF objects to center text in a rectangle."
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   12, _
   FontStyle.Bold, _
   GraphicsUnit.Point)
Dim rect As New Rectangle(30, 10, 120, 140) 
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

' Center each line of text.
stringFormat.Alignment = StringAlignment.Center

' Center the block of text (top to bottom) in the rectangle.
stringFormat.LineAlignment = StringAlignment.Center

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

Dim pen As Pen = Pens.Black
e.Graphics.DrawRectangle(pen, rect)
[C#]
string text = "Use StringFormat and RectangleF objects to center text in a rectangle.";
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   12, FontStyle.Bold,
   GraphicsUnit.Point);
Rectangle rect = new Rectangle(30, 10, 120, 140);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

// Center each line of text.
stringFormat.Alignment = StringAlignment.Center;

// Center the block of text (top to bottom) in the rectangle.
stringFormat.LineAlignment = StringAlignment.Center;

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

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

The following illustration shows a rectangle and the centered text.

332kzs7c.csfontstext3(en-us,VS.71).gif

The preceding code sets two properties of the StringFormat object: Alignment and LineAlignment. The Alignment property specifies that each line of text is centered horizontally across the rectangle passed to the DrawString method. The LineAlignment property specifies that the block of text is centered vertically (top to bottom) in the rectangle.

The value Center is a member of the StringAlignment enumeration.

Setting Tab Stops

You can set tab stops for text by calling the SetTabStops method of a StringFormat object and then passing that StringFormat object to the DrawString method of the Graphics class.

The following example sets tab stops at 150, 250, and 350. Then the code displays a tabbed list of names and test scores.

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)
[C#]
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);

The following illustration shows the tabbed text.

332kzs7c.fontstext4(en-us,VS.71).gif

The preceding code passes two arguments to the SetTabStops method. The second argument is an array that contains tab offsets. The first argument passed to SetTabStops is 0, which indicates that the first offset in the array is measured from position 0, the left edge of the bounding rectangle.

Drawing Vertical Text

You can use a StringFormat object to specify that text be drawn vertically rather than horizontally.

The following example assigns the value DirectionVertical to the FormatFlags property of a StringFormat object. That StringFormat object is passed to the DrawString method of the Graphics class. The value DirectionVertical is a member of the StringFormatFlags enumeration.

Dim myText As String = "Vertical text"

Dim fontFamily As New FontFamily("Lucida Console")
Dim font As New Font( _
   fontFamily, _
   14, _
   FontStyle.Regular, _
   GraphicsUnit.Point)
Dim pointF As New PointF(40, 10)
Dim stringFormat As New StringFormat()
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

stringFormat.FormatFlags = StringFormatFlags.DirectionVertical

e.Graphics.DrawString(myText, font, solidBrush, pointF, stringFormat)

[C#]
string text = "Vertical text";

FontFamily fontFamily = new FontFamily("Lucida Console");
Font font = new Font(
fontFamily,
   14,
   FontStyle.Regular,
   GraphicsUnit.Point);
PointF pointF = new PointF(40, 10);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

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

The following illustration shows the vertical text.

332kzs7c.csfontstext5(en-us,VS.71).gif

Show:
© 2014 Microsoft