Results 1 to 9 of 9

Thread: Printing example, short, simple & easy.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Printing example, short, simple & easy.

    After looking on the internet i have found some code for printing the text of a richtextbox. I added comments and added a few things...

    Commented Code:

    vb.net Code:
    1. 'NOTE * // = opening comments and \\ = closing comments...
    2.  
    3. Public Class Form1
    4.     'Declares public variables...
    5.     Public printer As String
    6.     Public copies As Integer
    7.     Private Sub cmdPrint_Click(ByVal sender As System.Object, _
    8.     ByVal e As System.EventArgs) Handles cmdPrint.Click
    9.         Dim Print As New myPrinter 'Declares Print as a new myPrinter class.
    10.         Dim myprintdialog As New PrintDialog 'Creates Print Dialog.
    11.         With myprintdialog
    12.             If .ShowDialog = Windows.Forms.DialogResult.OK Then
    13.                 '//------
    14.                 printer = .PrinterSettings.PrinterName 'Sets variable printer to selected
    15.                 'printers name.
    16.                 '\\------
    17.                 '//------
    18.                 copies = .PrinterSettings.Copies 'Sets ammount of copies to number specified
    19.                 ' in dialog.
    20.                 '\\------
    21.                 '//------
    22.                 Print.prt(rtbText.Text.Trim) 'calls the prt sub in
    23.                 'the myPrinter class with text (a string) set to the text in the rtb.
    24.                 'Basicly starts the printing process...
    25.                 '\\------
    26.             End If
    27.         End With
    28.     End Sub
    29. End Class
    30.  
    31. 'Print Class
    32. Public Class myPrinter
    33.     Friend TextToBePrinted As String 'Declares TextToBePrinted as a string.
    34.     'Below is the sub that prints the text to the printer.
    35.     Public Sub prt(ByVal text As String)
    36.         TextToBePrinted = text
    37.         Dim prn As New Printing.PrintDocument
    38.         Using (prn)
    39.             prn.PrinterSettings.PrinterName = Form1.printer
    40.             prn.PrinterSettings.Copies = Form1.copies
    41.             '// Adds a handler for PrintDocument.PrintPage
    42.             '(the sub PrintPageHandler)
    43.             AddHandler prn.PrintPage, _
    44.                AddressOf Me.PrintPageHandler
    45.             '\\
    46.             prn.Print() 'Prints.
    47.             '// Removes the handler for PrintDocument.PrintPage
    48.             '(the sub PrintPageHandler)
    49.             RemoveHandler prn.PrintPage, _
    50.                AddressOf Me.PrintPageHandler
    51.             '\\
    52.         End Using
    53.     End Sub
    54.     'Below is code that sets the fonts etc...
    55.     Private Sub PrintPageHandler(ByVal sender As Object, _
    56.        ByVal args As Printing.PrintPageEventArgs)
    57.         Dim myFont As New Font("Microsoft San Serif", 10)
    58.         args.Graphics.DrawString(TextToBePrinted, _
    59.            New Font(myFont, FontStyle.Regular), _
    60.            Brushes.Black, 50, 50)
    61.     End Sub
    62. End Class

    Not Commented Code:

    vb.net Code:
    1. Public Class Form1
    2.     Public printer As String
    3.     Public copies As Integer
    4.     Private Sub cmdPrint_Click(ByVal sender As System.Object, _
    5.     ByVal e As System.EventArgs) Handles cmdPrint.Click
    6.         Dim Print As New myPrinter
    7.         Dim myprintdialog As New PrintDialog
    8.         With myprintdialog
    9.             If .ShowDialog = Windows.Forms.DialogResult.OK Then
    10.                 printer = .PrinterSettings.PrinterName
    11.                 copies = .PrinterSettings.Copies
    12.                 Print.prt(rtbText.Text.Trim)
    13.             End If
    14.         End With
    15.     End Sub
    16. End Class
    17.  
    18. Public Class myPrinter
    19.     Friend TextToBePrinted As String
    20.     Public Sub prt(ByVal text As String)
    21.         TextToBePrinted = text
    22.         Dim prn As New Printing.PrintDocument
    23.         Using (prn)
    24.             prn.PrinterSettings.PrinterName = Form1.printer
    25.             prn.PrinterSettings.Copies = Form1.copies
    26.             AddHandler prn.PrintPage, _
    27.                AddressOf Me.PrintPageHandler
    28.             prn.Print()
    29.             RemoveHandler prn.PrintPage, _
    30.                AddressOf Me.PrintPageHandler
    31.         End Using
    32.     End Sub
    33.     Private Sub PrintPageHandler(ByVal sender As Object, _
    34.        ByVal args As Printing.PrintPageEventArgs)
    35.         Dim myFont As New Font("Microsoft San Serif", 10)
    36.         args.Graphics.DrawString(TextToBePrinted, _
    37.            New Font(myFont, FontStyle.Regular), _
    38.            Brushes.Black, 50, 50)
    39.     End Sub
    40. End Class

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Printing example, short, simple & easy.

    If anyone has any other printing code and you think it is better then the code i supplied please reply with the code or a link.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Printing example, short, simple & easy.

    I will update my code soon and make it more flexible, for printing pictures, different varies of fonts in one page etc...

    Right now it is a very simple example.

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Printing example, short, simple & easy.

    For info, there is code that does the printing pictures, different fonts and so on in this codebank item...

  5. #5
    Lively Member
    Join Date
    Jul 2009
    Posts
    78

    Re: Printing example, short, simple & easy.

    I tried using your code, it throws an error at:

    The error says:Type 'myPrinter' is not defined.

    VB Code:
    1. Dim Print As myPrinter 'Declares Print as a new myPrinter class.
    Last edited by veebienewbie; Jul 23rd, 2009 at 12:23 PM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Printing example, short, simple & easy.

    Quote Originally Posted by veebienewbie View Post
    I tried using your code, it throws an error at:

    The error says:Type 'myPrinter' is not defined.

    VB Code:
    1. Dim Print As myPrinter 'Declares Print as a new myPrinter class.
    You should use the New keyword.

    Code:
    Dim Print As New myPrinter

  7. #7
    Lively Member
    Join Date
    Jul 2009
    Posts
    78

    Re: Printing example, short, simple & easy.

    Where do I get it from?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Printing example, short, simple & easy.

    Quote Originally Posted by veebienewbie View Post
    Where do I get it from?
    I made a mistake, see post #6 again, I made an edit.
    Anyway the class myPrinter is supplied in the example.

  9. #9
    New Member
    Join Date
    Jan 2024
    Posts
    1

    Re: Printing example, short, simple & easy.

    Thread starter, Mate you wrote the simple and easy printing code in 2008... 16 years on I do thank you. hope you are travelling well.
    Printing example, short, simple & easy. the highest praise in science is to say your work is eloquent. Your work my friend is eloquent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width