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
 
 
 
 
 
TitlePrint a form's Labels and TextBoxes
DescriptionThis example shows how to print a form's Labels and TextBoxes in Visual Basic 6. It loops through the form's controls and prints those that are Labels and TextBoxes in their appropriate positions on the form.
KeywordsPrintForm, print form, Label, TextBox
CategoriesGraphics
 
Subroutine PrintFormFields loops through the form's controls. For Labels and TextBoxes, it calls subroutine PrintText.

Subroutine PrintText figures out where the control should be drawn and draws its text. Then if the draw_box parameter is True (it is for TextBoxes), it draws a box around the control.

 
' Print the Labels and TextBoxes.
Private Sub PrintFormFields(ptr As Object, frm As Form, _
    draw_box As Boolean)
Dim ctl As Control
Dim wid As Single
Dim hgt As Single

    For Each ctl In frm.Controls
        If TypeOf ctl Is Label Then
            PrintText ptr, frm, ctl, ctl.Caption, False
        ElseIf TypeOf ctl Is TextBox Then
            PrintText ptr, frm, ctl, ctl.Text, True
        End If
    Next ctl

    If draw_box Then
        wid = frm.ScaleX(frm.ScaleWidth, frm.ScaleMode, _
            vbTwips)
        hgt = frm.ScaleY(frm.ScaleHeight, frm.ScaleMode, _
            vbTwips)
        ptr.Line (0, 0)-Step(wid, hgt), , B
    End If
End Sub

' Print text where the control belongs.
Private Sub PrintText(ptr As Object, frm As Form, ctl As _
    Control, txt As String, draw_box As Boolean)
Dim l As Single
Dim t As Single
Dim wid As Single
Dim hgt As Single

    l = frm.ScaleX(ctl.Left, frm.ScaleMode, vbTwips)
    t = frm.ScaleY(ctl.Top, frm.ScaleMode, vbTwips)
    If draw_box Then
        ptr.CurrentX = l + _
            ScaleX(0.2 * ctl.Font.size, vbPoints, vbTwips)
        ptr.CurrentY = t + _
            ScaleY(0.2 * ctl.Font.size, vbPoints, vbTwips)
    Else
        ptr.CurrentX = l
        ptr.CurrentY = t
    End If
    
    ' Select the printer font.
    ptr.Font.Name = ctl.Font.Name
    ptr.Font.size = ctl.Font.size

    ptr.Print txt
    If draw_box Then
        wid = frm.ScaleX(ctl.Width, frm.ScaleMode, vbTwips)
        hgt = frm.ScaleY(ctl.Height, frm.ScaleMode, vbTwips)
        ptr.Line (l, t)-Step(wid, hgt), , B
    End If
End Sub
 
Drawing text and lines on the Printer in this way is much faster than dumping a whole bitmap image of the form to the printer.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated