Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePosition tab stops while drawing text in VB .NET
DescriptionThis example shows how to position tab stops while drawing text in VB .NET.
Keywordstabs, VB .NET, drawing, layout rectangle, StringFormat
CategoriesVB.NET, Graphics
 
When the form's Paint event handler fires, the program makes a layout rectangle that defines the area where the text should be drawn. It creates a StringFormat object to determine how the text is formatted. It uses this object's SetTabStops method to determine where the tab stops are..

The code then clears the drawing area and uses the Graphics object's DrawString method to draw the text. The layout rectangle and StringFormat tell DrawString where to put the text and how to handle tab characters.

The program finishes by drawing lines to show where the tab stops are.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Create the text we will display.
    Dim txt As String = ""
    txt &= "Bacon Burger" & vbTab & "3.00" & vbCrLf
    txt &= vbTab & "Tomato" & vbTab & "0.15" & vbCrLf
    txt &= vbTab & "Extra Cheese" & vbTab & "0.20" & vbCrLf
    txt &= "Large Fries" & vbTab & "0.99" & vbCrLf
    txt &= "Medium Softdrink" & vbTab & "1.05" & vbCrLf
    txt &= "" & vbCrLf
    txt &= "Subtotal" & vbTab & "5.39" & vbCrLf
    txt &= "Tax" & vbTab & vbTab & "0.38" & vbCrLf
    txt &= "Total" & vbTab & vbTab & "5.77" & vbCrLf

    ' Make a LayoutRectangle that coves the form.
    Dim layout_rect As New RectangleF( _
        Me.ClientRectangle.Left, _
        Me.ClientRectangle.Top, _
        Me.ClientRectangle.Width, _
        Me.ClientRectangle.Height)

    ' Create a StringFormat that defines the tab stops.
    Dim string_format As New StringFormat
    Dim tab_stops() As Single = {30, 130 - 30}
    string_format.SetTabStops(0, tab_stops)

    ' Clear and draw the text.
    e.Graphics.Clear(Me.BackColor)
    e.Graphics.DrawString(txt, Me.Font, _
        Brushes.Black, layout_rect, string_format)

    ' Show where the tabs are.
    e.Graphics.DrawLine(Pens.Red, 30, 0, 30, _
        Me.ClientSize.Height)
    e.Graphics.DrawLine(Pens.Red, 130, 0, 130, _
        Me.ClientSize.Height)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated