locked
Printing in VB 2010

    Question

  • Migrating from VB6 to VB 2010 I am finding difficulties to understand printing methods. The following piece of code in VB6 is used for printing a table. 

    I would like to know to do this in VB 2010.

    (db1 is the database, rs1 is the recordset)

    criterio = "dia>#" & Format(escolha, "m/d/yy") & "#"

    rs1.FindFirst critério

     

    dim inicial as integer = 0

    dim coluna as integer = 10

    dim linha as integer = 10

    Printer.ScaleMode = 6

    Printer.Orientation = 2

    Printer.CurrentY = inicial

    Printer.CurrentX = coluna

    Printer.FontName = "Times new roman"

    Printer.FontSize = 8

    Printer.FontBold = False

    Printer.FontItalic = False

    Do While Not rs1.EOF

    Printer.Print rs1!numero

        Printer.CurrentX = coluna + 15

        Printer.CurrentY = linha

        Printer.Print rs1!material

        Printer.CurrentX = coluna + 82

        Printer.CurrentY = linha

        Printer.Print rs1!codigo

        Printer.CurrentX = coluna + 137

        Printer.CurrentY = linha

        Printer.Print rs1!notafiscal

        Printer.CurrentX = coluna + 154

        Printer.CurrentY = linha

        Printer.Print rs1!cep

        Printer.CurrentX = coluna + 172

        Printer.CurrentY = linha

        Printer.Print rs1!qtidade

        Printer.CurrentX = coluna + 190

        Printer.CurrentY = linha

        Printer.Print rs1!dia

        linha = linha + 5

        rs1.MoveNext

    Loop

    Printer.EndDoc

    db1.Close

    Thanks.

    • Moved by Jennifer Zhan Wednesday, September 22, 2010 1:49 AM (From:Visio General Questions and Answers for IT Professionals)
    Tuesday, September 21, 2010 11:58 AM

Answers

  • Thank you Al Edlund for your friendly support!

     

    Hi MarcosLeite,

     

    Welcome to MSDN forums.

     

    Here are two Print approaches frequently-used in VB.NET programming:

     

    1.      Using PrintForm component in Visual Basic Power Packs

    The PrintForm component is designed to bring back the ability to easily print a Windows Form. With it, you can layout the Windows Form exactly as you want it and allow you to print the form as a quick report.

    Microsoft Visual Basic Power Packs 3.0 Download

    http://msdn.microsoft.com/en-us/vbasic/bb735936.aspx

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

        End Sub

     

     

    2.      Using PrintDocument, PrintDialog components

    The .NET Framework provides excellent support for Printing documents.

    PrintDocument Component:

    In the .NET Framework, a printed document is represented by the PrintDocument component. The PrintDocument object encapsulates all the information needed to print a page. They handle the events and operations of printing.

     

    PrintDialog Component:

    To print a document, we need to set the PrintDialog’s Document property to PrintDocument object, and use the PrintDocument object's Print method. To send content to the printer, you must handle this PrintDocument_PrintPage event to render the content.

        Private Sub buttonPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim printDialog1 As PrintDialog = New PrintDialog

            printDialog1.Document = printDocument1

            Dim result As DialogResult = printDialog1.ShowDialog

            If (result = DialogResult.OK) Then

                printDocument1.Print()

            End If

        End Sub

     

    Code sample: How to use PrintDocument component to Print multiline and scrollable TextBox in VB.NET.
    http://social.msdn.microsoft.com/Forums/en-US/vbpowerpacks/thread/2ad34554-e989-4ffa-8e1f-ccea1f00c99d

     

    How Printing Works?

    Printing content is provided directly by the application logic in the .NET Framework. You add a PrintDocument object and handle the PrintPage event which is called every time when a new page is to be printed. A print job is initiated by the PrintDocument's Print method. This starts the print job and raises one or more events. When the print job begins, a BeginPrint event occurs, followed by the PrintPage event for each page, followed by the EndPage event when the job is done. If the print job contains multiple pages, one PrintPage event will be raised for each page in the job making the PrintPage event to execute multiple times. The PrintPage event is the main event involved in printing documents. To send content to the printer, you must handle this PrintPage event and provide code to render the content in the PrintPage event handler.

     

    Some tutorial:

    Article: Printing feature in .NET Framework

    http://www.startvbdotnet.com/controls/printdialog.aspx

    Windows Forms Printing

    http://www.syncfusion.com/faq/windowsforms/faq_c55c.aspx#q491q

    How to print the content of RichTextBox using Visual Basic .NET?

    http://support.microsoft.com/kb/811401

     

     

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 22, 2010 9:51 AM
    Moderator

All replies

  • This is the Visio forum, you might try this in the Visual Studio forum.

    al

     

    Tuesday, September 21, 2010 12:22 PM
  • Thank you Al Edlund for your friendly support!

     

    Hi MarcosLeite,

     

    Welcome to MSDN forums.

     

    Here are two Print approaches frequently-used in VB.NET programming:

     

    1.      Using PrintForm component in Visual Basic Power Packs

    The PrintForm component is designed to bring back the ability to easily print a Windows Form. With it, you can layout the Windows Form exactly as you want it and allow you to print the form as a quick report.

    Microsoft Visual Basic Power Packs 3.0 Download

    http://msdn.microsoft.com/en-us/vbasic/bb735936.aspx

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

        End Sub

     

     

    2.      Using PrintDocument, PrintDialog components

    The .NET Framework provides excellent support for Printing documents.

    PrintDocument Component:

    In the .NET Framework, a printed document is represented by the PrintDocument component. The PrintDocument object encapsulates all the information needed to print a page. They handle the events and operations of printing.

     

    PrintDialog Component:

    To print a document, we need to set the PrintDialog’s Document property to PrintDocument object, and use the PrintDocument object's Print method. To send content to the printer, you must handle this PrintDocument_PrintPage event to render the content.

        Private Sub buttonPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim printDialog1 As PrintDialog = New PrintDialog

            printDialog1.Document = printDocument1

            Dim result As DialogResult = printDialog1.ShowDialog

            If (result = DialogResult.OK) Then

                printDocument1.Print()

            End If

        End Sub

     

    Code sample: How to use PrintDocument component to Print multiline and scrollable TextBox in VB.NET.
    http://social.msdn.microsoft.com/Forums/en-US/vbpowerpacks/thread/2ad34554-e989-4ffa-8e1f-ccea1f00c99d

     

    How Printing Works?

    Printing content is provided directly by the application logic in the .NET Framework. You add a PrintDocument object and handle the PrintPage event which is called every time when a new page is to be printed. A print job is initiated by the PrintDocument's Print method. This starts the print job and raises one or more events. When the print job begins, a BeginPrint event occurs, followed by the PrintPage event for each page, followed by the EndPage event when the job is done. If the print job contains multiple pages, one PrintPage event will be raised for each page in the job making the PrintPage event to execute multiple times. The PrintPage event is the main event involved in printing documents. To send content to the printer, you must handle this PrintPage event and provide code to render the content in the PrintPage event handler.

     

    Some tutorial:

    Article: Printing feature in .NET Framework

    http://www.startvbdotnet.com/controls/printdialog.aspx

    Windows Forms Printing

    http://www.syncfusion.com/faq/windowsforms/faq_c55c.aspx#q491q

    How to print the content of RichTextBox using Visual Basic .NET?

    http://support.microsoft.com/kb/811401

     

     

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 22, 2010 9:51 AM
    Moderator
  • In addition, if you just want to print an external document in VB.NET, you can use Process.Start method as below code sample:

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim psi As New ProcessStartInfo

            psi.UseShellExecute = True

            psi.Verb = "print"

            psi.WindowStyle = ProcessWindowStyle.Hidden

    'psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()

            psi.FileName = "C:\MyFile.doc" ' Here specify a document to be printed

            Process.Start(psi)

        End Sub

    End Class

     

     

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, September 22, 2010 9:53 AM
    Moderator
  • Thanks, I will try the other forum
    Wednesday, September 22, 2010 2:29 PM
  • Thanks Martin

    I will try your suggestions.

    Wednesday, September 22, 2010 2:29 PM
  • Mr. Martin_Xie,

     

    good afternoon i thank you for all your help. I have a problem, when i try to print a from using " Private Sub buttonPrint_Click(ByVal sender As ObjectByVal e As System.EventArgs)

            Dim printDialog1 As PrintDialog = New PrintDialog

            printDialog1.Document = printDocument1

            Dim result As DialogResult = printDialog1.ShowDialog

            If (result = DialogResult.OK) Then

                printDocument1.Print()

            End If

        End Sub"

     

    the print comes empty, with no form, just white paper. I tryed also printing like this:

     

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click

     

     

            PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview

            PrintForm1.Print()

     

     

    and used 

     

         e.PageSettings.Landscape = True

     

     

    but it still does not feet the entire page, the right side is cutted.

     

    I prefer using your idea but what can i do about the printing comming empty.

     

    Sérgio

     

     

        End Sub

    Friday, September 16, 2011 12:42 PM