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

How do I print in Visual Basic .NET 2005

Posted on 2006-07-10
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
  • 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.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


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 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

789 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