How do I print in Visual Basic .NET 2005

Posted on 2006-07-10
Medium Priority
Last Modified: 2010-04-23
I have a program that processes a lot of information. It produces the results in labels and text boxes from variables.

I would like a formatted printout of the information from the variables. How Do I do this?

Thank you all very much.
Question by:OsirisJa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 17078676
Hi  OsirisJa

Check here for some very basics on printing in VS.Net



Author Comment

ID: 17078901
I apologize, but I am lost.

Could you help me with a subroutine that displays the select printer dialog and allows printing to a Printer on the system?

I would also like the code required to print and then the foll examples

Print the contents of a variable to the page
Priint a text string to the page
Print Centred text to the page
and any other things important to know.

Thank you very much


Expert Comment

ID: 17078985
I think this is a good article to get some basics of printing and print dialog.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 17080673
Thats is not using VB.NET 2005 and it does not demonstrate how to

Print the contents of a variable to the page
Priint a text string to the page
Print Centred text to the page
and any other things important to know.

Please help me directly address my question and not point me to a random link.

Thank you

Accepted Solution

jrscherer earned 1500 total points
ID: 17087664
here are some examples

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim verticalLocation As Single = 100.0 ' one inch from left border
        Dim horizontalLocation As Single = 200.0 ' 2 inches from top border

        Dim MyFont As New Font("Arial", 10, FontStyle.Regular)
        ' prepare the variable Integer1 to be printed
        e.Graphics.DrawString(Variable1.ToString, MyFont, Brushes.Black, horizontalLocation, verticalLocation)

        verticalLocation = 150.0

        ' prepare the variable String1 to be printed
        e.Graphics.DrawString(Variable1.ToString, MyFont, Brushes.Black, horizontalLocation, verticalLocation)

        verticalLocation = 200.0

        ' center the string ad midscreen
        ' first measure the with of the string when printed
        Dim stringWidth As Single = e.Graphics.MeasureString(String1, MyFont).Width

        ' then locate the midpoint of the paper
        Dim MidPoint As Single = CSng(e.PageBounds.Left + (e.PageBounds.Width / 2))

        ' then prepare printing of the string centered into the page, respecting page setting of the printer setting, such as paper size and orientation
        e.Graphics.DrawString(Variable1.ToString, MyFont, Brushes.Black, MidPoint - (stringWidth / 2), verticalLocation)
        ' another string formatting method follows
        ' draw a string and use the GDI+ string formatter
        Dim frm As New StringFormat
        frm.Alignment = StringAlignment.Center
        frm.LineAlignment = StringAlignment.Center

        ' define a rectangle to draw a centered text
        Dim Rec As RectangleF
        Rec.Location = New PointF(horizontalLocation, 250)
        Rec.Size = New Size(200, 200)

        ' draw the rectangle as reference
        e.Graphics.DrawRectangle(Pens.Gray, Rec.X, Rec.Y, Rec.Width, Rec.Height) ' no overload exists fro RectangleF

        ' draw the centered text into box
        e.Graphics.DrawString(String1, MyFont, Brushes.Black, Rec, frm) ' frm does the formatting to the rectangle

  End Sub

Hope this helps to find the way into printing  Jack.net

Author Comment

ID: 17105950
Thank you very much. It wasn't exactly what I wanted, but I was able to use pieces from it and it gave me a general idea.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question