Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Print to Dot Matrix in VB.NET

4.14/5 (4 votes)
23 Oct 2014CPOL 21.9K  
Print to DotMatrix in VB.NET

Introduction

I was recently asked to print lines of text from a VB.NET application to a dot matrix printer. This tip is very basic so please bear with me.

Using the Code

Create a new Windows Form in Visual Studio.

  • Add button control (just drag onto window)
  • Add printdocument control (just drag onto window)
  • Drag printDialog onto control

On default, the printdocument control will be named "PrintDocument1".
On button click event, you can add:

C++
If PrintDialog1.showDialog() = DialogResult.OK then
   PrintDocument1.print()
end if

As seen in this code, the PrintDocument1 gets printed. But what is in this document?

Either double click on the PrintDocument1 to open the PrintPage event or open from event viewer.

VB.NET
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

e.Graphics.DrawString(textbox1.Text, New Font("Arial", 10), Brushes.Black, New Point(100, 795))

End Sub

This will then print the string in the textbox to the dot matrix printer. You should define the font and font size. The 2 numbers at the end are the position of X and Y. "New Point(100, 795))"
This way, you can move the text to the corresponding position where it is needed.

You can also add multiple lines to print from:

VB.NET
e.Graphics.DrawString("Offload To: " & _
txtOffloadAt.Text, New Font("Arial", 10), Brushes.Black, New Point(50, 250))
e.Graphics.DrawString(txtLDSpecialIns.Text, _
New Font("Arial", 10), Brushes.Black, New Point(50, 360))
e.Graphics.DrawString(DateTimePicker1.Text, _
New Font("Arial", 10), Brushes.Black, New Point(630, 220))

I hope this will help someone new to printing from dot matrix printers.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)