Click here to Skip to main content
15,908,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have designed an invoice software in vb.net by using vb.net and datagrid view.................but i am not able to print it like a good invoice should b printed,,only the form portion is being printed,,can you help to produce an invoice after printing it.
Any help will be appreciated
The code is as follows
Imports System.Data.SqlClient
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents printButton As System.Windows.Forms.Button

    Private printFont As Font
    Private streamToPrint As StreamReader
    Dim connetionString As String
    Public Sub New()
        ' The Windows Forms Designer requires the following call.
        InitializeComponent()
        InitializeForm()
    End Sub
    Private Sub InitializeForm()
        Me.components = New System.ComponentModel.Container()
        Me.Button2 = New System.Windows.Forms.Button()

        Me.ClientSize = New System.Drawing.Size(800, 600)
        Me.Text = "Print Example"

        Button2.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Button2.Location = New System.Drawing.Point(12, 612)
        Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Button2.TabIndex = 0
        Button2.Text = "Print the file."
        Button2.Size = New System.Drawing.Size(139, 23)
        AddHandler Button2.Click, AddressOf Button2_Click

        Me.Controls.Add(Button2)
    End Sub

    ' The PrintPage event is raised for each page to be printed.
    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing

        ' Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

        ' Print each line of the file.
        While count < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            count += 1
        End While

        ' If more lines exist, print another page.
        If (line IsNot Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub




    

 
   

            Dim reply As DialogResult = MessageBox.Show("Do You want to print the Final Invoice", "Want the final Invoice Now !!", _
                  MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
            If reply = Windows.Forms.DialogResult.Yes Then
                PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
                PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
                Button1.Visible = True
            End If

        End If

        Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog

        Me.SuspendLayout()




    End Sub

 

    End Sub


   


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            streamToPrint = New StreamReader("C:\MyFile.txt")
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
                pd.Print()
            Finally
                streamToPrint.Close()
          
    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class


[edit]Inline code converted to Code block - OriginalGriff[/edit]
Posted
Updated 20-Jul-11 2:26am
v7
Comments
Dave Kreskowiak 19-Jul-11 10:09am    
You REALLY need to go back and review your code. If this is a production application, you're control names are poor at best. How do you what textboxes and used for what just by looking at the control names? I feel sorry for the poor schmuck that's going to have to come in after you and try and support this code.
dks1383 21-Jul-11 13:24pm    
HiI'm interested in your program and I wanted to run and test your program to understand it. but your code has some errors:
Dim reply As DialogResult = MessageBox.Show("Do You want to print the Final Invoice", "Want the final Invoice Now !!"
Is outside of a method….
onkarnath2001 25-Jul-11 1:02am    
hi Member 4686760,
i had written the complete code but A member asked to write the code section only where i am getting problem,not the complete code.so,i trimmed it.

Don't do it that way. PrintForm is ok -ish- but you really want to do the job properly. Use a PrintDocument[^] instead - it provides a lot better control, and well as looking better as a printed object. The link includes an example.
 
Share this answer
 
Comments
onkarnath2001 19-Jul-11 7:38am    
Griff thanks for your reply but i want a print as a real invoice looks like
OriginalGriff 19-Jul-11 7:45am    
That is why you should use a PrintDocument - that is what it is there for. It allows you to specify every little detail in a way which is document (i.e. invoice) oriented, rather than as part of a user interface.
onkarnath2001 19-Jul-11 8:36am    
I FOLLOWED THE LINK BUT IT IS PRINTING A BLANK DOCUMENT...KINDLY HELP
OriginalGriff 19-Jul-11 8:58am    
No need to shout! :laugh:
Did you try putting anything on it? ev.Graphics.DrawString in the example...
onkarnath2001 20-Jul-11 3:43am    
Where to add this one???please give me the details ...
u should use crystal reports ....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900