ARTICLE

Printing in Visual Basic 2005

Posted by Sabith Kp Articles | Visual Basic 2010 July 12, 2007
The code in this article shows how to print items from listview in my inventory software.
 
Reader Level:

I wrote the below code for printing sales and purchase data in my software.

In vb 2005 printing is very easy.This is the one of the simple way to print in VB2005.

'Printing in VB 2005 Printing Data From ListView

Imports System.Drawing.Printing

Dim tableFont, titlefont, headfont As Font
Dim X1, X2, X3 As Integer
Dim
W1, W2, W3 As Integer
Dim
Y As Integer
Dim
itm As Integer

'I Create this Sub for set the Fontname,FontSize,Paper Setting

Public Sub PrintBIll()

Dim PSize As Integer = ListItems.Items.Count
Dim PHi As Double
With PrintDocument1.DefaultPageSettings
Dim Ps As PaperSize
PHi = PSize * 20 + 350
Ps =
New PaperSize("Cust", 800, PHi).Margins.Top = 15.Margins.Bottom = 20.PaperSize = Ps
End With

headfont = New Font("Courier New", 16, FontStyle.Bold)
tableFont =
New Font("Courier New", 10)
titlefont =
New Font("Courier New", 12, FontStyle.Bold)
X1 = PrintDocument1.DefaultPageSettings.Margins.Left
Dim pageWidth As Integer
With PrintDocument1.DefaultPageSettings pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
End With
X2 = X1 + 120
X3 = X2 + pageWidth * 0.5
W1 = X2 - X1
W2 = X3 - X2
W3 = pageWidth - X3

'If u want to print Directly then

PrintDocument1.Print()

'Elseif u want print after print preview then u pls use this code

PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
itm = 0

End Sub

'==============================================

'Printing Current Documents

 

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles

    PrintDocument1.PrintPage()

Y = PrintDocument1.DefaultPageSettings.Margins.Top + 10 e.Graphics.DrawString("INVOICE", headfont, Brushes.Black, X1 + 250, Y)

    e.Graphics.DrawString("Cash/Credit Bill ", titlefont, Brushes.Black, X1, Y + 50)

    e.Graphics.DrawString("To : " & txtCustomername.Text, titlefont, Brushes.Black, X1, Y + 80)

    e.Graphics.DrawString("Date : " & Format(DTsale.Value, "dd-MM-yyyy"), titlefont, Brushes.Black, X1 + 450, Y + 25)

    e.Graphics.DrawString("Bill No : " & txtReference.Text, titlefont, Brushes.Black, X1 + 450, Y + 50)

    Y = PrintDocument1.DefaultPageSettings.Margins.Top + 120

    With PrintDocument1.DefaultPageSettings

          e.Graphics.DrawLine(Pens.Black, .Margins.Left, Y + 20, _ .PaperSize.Width - .Margins.Right, Y + 20)

    End With

e.Graphics.DrawString("SNO", titlefont, Brushes.Black, X1, Y) e.Graphics.DrawString("NAME", titlefont, Brushes.Black, X2 - 50, Y)

    e.Graphics.DrawString("QTY", titlefont, Brushes.Black, X2 + 220, Y)

    e.Graphics.DrawString("RATE", titlefont, Brushes.Black, X2 + 280, Y)

    e.Graphics.DrawString("MRP", titlefont, Brushes.Black, X2 + 345, Y)

    e.Graphics.DrawString("TOTAL", titlefont, Brushes.Black, X2 + 425, Y)

    Y = Y + 30

    While itm < ListItems.Items.Count

        Dim str As String

        'str = ListItems.Items(itm).Text

        e.Graphics.DrawString(itm + 1, tableFont, Brushes.Black, X1, Y)

        str = ListItems.Items(itm).SubItems(1).Text

        Dim R As New RectangleF(X2 - 50, Y, W2, 80)

        e.Graphics.DrawString(str, tableFont, Brushes.Black, R) 

        str = ListItems.Items(itm).SubItems(4).Text

        Dim k As New RectangleF(X2 + 220, Y, W2, 80)

        e.Graphics.DrawString(str, tableFont, Brushes.Black, k)

        str = ListItems.Items(itm).SubItems(5).Text

        Dim M As New RectangleF(X2 + 235, Y, W2, 80)

        e.Graphics.DrawString(str, tableFont, Brushes.Black, M)

        str = Format(ListItems.Items(itm).SubItems(7).Text, "Fixed")

        Dim N As New RectangleF(X2 + 280, Y, W2, 80)

        e.Graphics.DrawString(str, tableFont, Brushes.Black, N)

        '''''' str = Format(ListItems.Items(itm).SubItems(6).Text, "Fixed")

        Dim L As New RectangleF(X2 + 340, Y, W2, 80)

        e.Graphics.DrawString(str, tableFont, Brushes.Black, L)

        str = SetSpacing(Format(ListItems.Items(itm).SubItems(8).Text, "Fixed"), 10)

        Dim O As New RectangleF(X2 + 400, Y, W2, 80)

        e.Graphics.DrawString(str, tableFont, Brushes.Black, O)

        Dim lines, Cols As Integer

        e.Graphics.MeasureString(str, tableFont, New SizeF(W2, 50), _ New StringFormat(), Cols, lines)

        Dim Yc As Integer

        Yc = Y

        Y = Y + lines * tableFont.Height + 5

        Y = Math.Max(Y, Yc)

        itm = itm + 1

    End While

    e.Graphics.DrawString("Net Amount : " & SetSpacing(Format(txtNetAmount.Text, "Fixed"), 10), titlefont, Brushes.Black, X2 + 240, Y + 15)

    e.Graphics.DrawString("Roundof : " & SetSpacing(Format(txtRounof.Text, "Fixed"), 10), titlefont, Brushes.Black, X2 + 240, Y + 35)

    e.Graphics.DrawString("Paid Amount : " & SetSpacing(Format(txtPaymentAmount.Text, "Fixed"), 10), titlefont, Brushes.Black, X2 + 240, Y + 55)

    e.Graphics.DrawString("Balance : " & SetSpacing(Format(txtbalance.Text, "Fixed"), 10), titlefont, Brushes.Black, X2 + 240, Y + 75)

    e.Graphics.DrawString("", titlefont, Brushes.White, X2 + 240, Y + 105)

 

End Sub

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks for your guidance and modification in code. Those errors have been removed.
Please advise that following line of code is also giving error:

str = SetSpacing(Format(Listitems.Items(itm).SubItems(8).Text, "Fixed"), 10)

e.Graphics.DrawString("Net Amount : " & SetSpacing(Format(txtNetAmount.Text, "Fixed"), 10), titlefont, Brushes.Black, X2 + 240, Y + 15)

The error is : The Name Setspacing is not declared.
Please advise fixing of this problem too.
Thanks for all of your guidance and expertise.

Posted by wasim Dec 21, 2009

Here is the modified code please check and fix your problem


Public Sub PrintBIll()

        Dim PSize As Integer = ListItems.Items.Count
        Dim PHi As Double
        With PrintDocument1.DefaultPageSettings
            Dim Ps As PaperSize
            PHi = PSize * 20 + 350
            Ps = New PaperSize("Cust", 800, PHi)
            .Margins.Top = 15
            .Margins.Bottom = 20
            .PaperSize = Ps
        End With

        headfont = New Font("Courier New", 16, FontStyle.Bold)
        tableFont = New Font("Courier New", 10)
        titlefont = New Font("Courier New", 12, FontStyle.Bold)
        X1 = PrintDocument1.DefaultPageSettings.Margins.Left
        Dim pageWidth As Integer
        With PrintDocument1.DefaultPageSettings
            pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
        End With
        X2 = X1 + 120
        X3 = X2 + pageWidth * 0.5
        W1 = X2 - X1
        W2 = X3 - X2
        W3 = pageWidth - X3

        'If u want to print Directly then

        PrintDocument1.Print()

        'Elseif u want print after print preview then u pls use this code

        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()
        itm = 0

    End Sub

Posted by Sabith Kp Dec 21, 2009

Here is the modified code please check and fix your problem


Public Sub PrintBIll()

        Dim PSize As Integer = ListItems.Items.Count
        Dim PHi As Double
        With PrintDocument1.DefaultPageSettings
            Dim Ps As PaperSize
            PHi = PSize * 20 + 350
            Ps = New PaperSize("Cust", 800, PHi)
            .Margins.Top = 15
            .Margins.Bottom = 20
            .PaperSize = Ps
        End With

        headfont = New Font("Courier New", 16, FontStyle.Bold)
        tableFont = New Font("Courier New", 10)
        titlefont = New Font("Courier New", 12, FontStyle.Bold)
        X1 = PrintDocument1.DefaultPageSettings.Margins.Left
        Dim pageWidth As Integer
        With PrintDocument1.DefaultPageSettings
            pageWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
        End With
        X2 = X1 + 120
        X3 = X2 + pageWidth * 0.5
        W1 = X2 - X1
        W2 = X3 - X2
        W3 = pageWidth - X3

        'If u want to print Directly then

        PrintDocument1.Print()

        'Elseif u want print after print preview then u pls use this code

        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()
        itm = 0

    End Sub

Posted by Sabith Kp Dec 21, 2009

I am trying to implement this code for printing ListView items and having following problems in the above code:

Ps = New PaperSize("Cust", 800, PHi).Margins.Top = 15.Margins.Bottom = 20.PaperSize = Ps
This line of code gives an error that Margins is not a member of System.Drawing.Printing.papersize

str = Spacing(Format(Listitems.Items(itm).SubItems(8).Text, "Fixed"), 10)
The above line of code gives an error that Spacing is not declared.
Please advise how i can fix these problems to implement the code successfully.
Thanks

Posted by wasim Dec 20, 2009

i wish someone will help me to make my project,,, i cant work with visual basic 2005 because i used to work with visual basic 0.6,, my project name is ( daily medicines consumption), so i need combobox include all medicines name (it's about 60 items) and 3 textbox (strength*frequency*duration) and another textbox to show the calculation result, for example (AMITRYPILINE 10 MG) (2)*(3)*(30) = (180) then button for next enter. and every time i enter new medicine the result will be added to the previous result (for each medicine separately). finaly, button to print all medicines names with the final consumptions result for each medicines separately for example: (amitryptiline 10 mg = 4500) (fluvoxamin 50 mg = 670) i wish my project is clear and i wish i can find help, at least some steps and codes... thanks everybody.... (mixa)

Posted by Ahmad gh Nov 28, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor