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]