Welcome to Hackers Alliance!

[VB.NET] Printing

  • Archived

    The forum is archived and used for testing. It is currently read-only to visitors.
    It has been upgraded from vBulletin 3.8.x to XenForo for security purposes and future-proofing. Proprietary code and modifications (such as code database and HA bot) are broken with XenForo and will stay only with vBulletin.

1UP

New member
Nov 3, 2010
66
3
0
33
This deals with setting up a page to actually print information to a printer. This was pry one of the biggest pain in the ass things I had to do (and really it's pry best to make a class out of this if you plan on using this for anything else in the future)

Lets get started:

On your form you are going to want to add 2 controls
-PrintPreviewDialouge
-PrintDocument

With that said we will now need to get the page setup for printing.


The code for the Print document is as follows

Code:
        Dim PrintFont As New Font("Arial", 12)
        Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold)
        Dim LineHeightSingle As Single = PrintFont.GetHeight + 2
        Dim ColumnHorizontalLocationSingle As Single = e.MarginBounds.Left
        Dim VerticlePrintLocationSingle As Single = e.MarginBounds.Top
        Dim Column2HorizontalLocationSingle As Single = 300
        Dim Column3HorizontalLocationSingle As Single = 500
        Dim PrintLineString As String
        Dim FontSizeF As New SizeF
        Dim FormattedPriceString As String
Let's explain some of this.
Printfont is pretty much explains itself. This is just where you will set your font you want to use and size.

Heading Font is the same thing, just a different name since it will be used for a different line on the paper.

Lineheight is the space between lines (much like how when you do something in word you set double line space to make the text readable)

Pretty much the rest deals with margins (so you dont have text slammed up on the edges of the paper.)

Now for a sample of what this would look like when you fill it out.

Code:
PrintLineString = "Store Shipping Summary"
        e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocationSingle)

        PrintLineString = "by Justin"
        VerticlePrintLocationSingle += LineHeightSingle
        e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocationSingle)
        VerticlePrintLocationSingle += LineHeightSingle * 2

        e.Graphics.DrawString("Weight", PrintFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocationSingle)
        e.Graphics.DrawString("Zone", PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticlePrintLocationSingle)

        FormattedPriceString = Format("Price")
        FontSizeF = e.Graphics.MeasureString(FormattedPriceString, PrintFont)
        Column3HorizontalLocationSingle = 555 - FontSizeF.Width

        e.Graphics.DrawString(FormattedPriceString, PrintFont, Brushes.Black, Column3HorizontalLocationSingle, VerticlePrintLocationSingle)
        VerticlePrintLocationSingle += LineHeightSingle
Now this is just a small sample of the code (the rest deals with other things the program was doing and you wouldnt get alot out of it without the entire program being listed, but an image should suffice.
 

Double_0_negative

Hackers Alliance Veteran
Administrator
Sep 18, 2010
444
8
0
wow, awesome! didn't know that you could print to the printer from vb.net.

i need learn some of this stuff, looks cool.
 

1UP

New member
Nov 3, 2010
66
3
0
33
wow, awesome! didn't know that you could print to the printer from vb.net.

i need learn some of this stuff, looks cool.
Everyone seems to think you are really limited with vb with what you can do, but you can really do pretty much anything
 
This site has been archived and is no longer accepting new content.

About us

  • Hackers Alliance is a small community forum about gaming and console hacking. Join our humble community to share ideas, game cheats, mods, and be part of an amazing growing community!

Quick Navigation

User Menu