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
  • 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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 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