Click here to Skip to main content
15,890,185 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Print to Dot Matrix in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.14/5 (4 votes)
23 Oct 2014CPOL 21.6K   5   1
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)


Written By
Software Developer
South Africa South Africa
ITIL Certified. As developer my work include new system designs and maintenance. Work with VB.NET, C#, XML, ASP.NET, javascript, Qlikview, Sharepoint, and many more...

Comments and Discussions

 
Questionhave you consider to post this as a tip? Pin
Nelek8-Nov-14 13:43
protectorNelek8-Nov-14 13:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.