Output text to a form

In Visual Basic, the printer is not the only thing you can print to! You can also print text and graphics to a form or PictureBox. The code below gives you an example. The code could also be adapted to print the page using the printer too.

Option Explicit
'// you can change the font name, size and colour by changing the 
'// form's Font, FontSize and ForeColor properties.
Private Sub Form_Load()
    Call DrawText
End Sub
Private Sub DrawText()
    Dim strText As String
    Me.Cls '// clear form
    'Never allow the percentage to be 0 or 100 unless it is exactly that value. This
    'prevents, for instance, the status bar from reaching 100% until we are entirely done.
    strText = "Contents" '// set the text
    SetCenterPos (strText) '// set the center pos
    Me.Print strText '// print the text
    strText = ""
    SetCenterPos (strText)
    Me.Print strText
    strText = "Chapter 1: A walk in the woods"
    SetCenterPos (strText)
    Me.Print strText
    strText = "Chapter 2: The suprise"
    SetCenterPos (strText)
    Me.Print strText
End Sub
Private Sub SetCenterPos(strText As String)
    Dim intX As Integer
    Dim intWidth As Integer
    intWidth = Me.TextWidth(strText)
    '// Set intX to the starting location for printing the percentage
    intX = Me.ScaleWidth / 2 - intWidth / 2
    '// Go to the center print position
    Me.CurrentX = intX
End Sub

You might also like...


James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou