| | |
how to print in vb.net
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
Here is code for Printing in VB.Net which we r using in our Project.
Its printing the document but not in tabular format which we require.
So try this code its working , but if u find any solution to print in receipt format.Plz let me knw.
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Try
Fs = New FileStream("D:\MyFile1.txt", FileMode.Create)
Sw = New StreamWriter(Fs)
Dim Row As Integer
'Row = myView.RowCount - 1
Sw.WriteLine("{0,5}{1,15}{2,20}{3,20}{4,20}", "SUPPLIER_ID", "SUPPLIER_NAME", "SUPPLIER_ADDRESS", "CONTACT_PERSON", "CONTACT_NUMBER")
Sw.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------------")
For Row = 0 To (myView.RowCount - 1)
Sw.WriteLine()
Sw.WriteLine("{0,5}{1,25}{2,35}{3,45}", ((myView.Item("SUPPLIER_ID", Row)).Value), ((myView.Item("SUPPLIER_NAME", Row)).Value), ((myView.Item("SUPPLIER_ADDRESS", Row)).Value), ((myView.Item("CONTACT_PERSON", Row)).Value)) ', "CONTACT_NUMBER")
Sw.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------")
Next
'MessageBox.Show(j)
'MessageBox.Show(i)
Sw.Close()
Fs.Close()
Fs1 = New FileStream("D:\MyFile1.txt", FileMode.Open)
Sr1 = New StreamReader(Fs1)
txtDisplay.Text = Sr1.ReadToEnd
Sr1.Close()
Fs1.Close()
Catch ex As Exception
'MessageBox.Show(ex.Message)
Finally
Sw = Nothing
Fs = Nothing
End Try
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
streamToPrint = New StreamReader("D:\MyFile1.txt")
Try
printFont = New Font("Arial", 10)
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.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 Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
Its printing the document but not in tabular format which we require.
So try this code its working , but if u find any solution to print in receipt format.Plz let me knw.
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Try
Fs = New FileStream("D:\MyFile1.txt", FileMode.Create)
Sw = New StreamWriter(Fs)
Dim Row As Integer
'Row = myView.RowCount - 1
Sw.WriteLine("{0,5}{1,15}{2,20}{3,20}{4,20}", "SUPPLIER_ID", "SUPPLIER_NAME", "SUPPLIER_ADDRESS", "CONTACT_PERSON", "CONTACT_NUMBER")
Sw.WriteLine("----------------------------------------------------------------------------------------------------------------------------------------------")
For Row = 0 To (myView.RowCount - 1)
Sw.WriteLine()
Sw.WriteLine("{0,5}{1,25}{2,35}{3,45}", ((myView.Item("SUPPLIER_ID", Row)).Value), ((myView.Item("SUPPLIER_NAME", Row)).Value), ((myView.Item("SUPPLIER_ADDRESS", Row)).Value), ((myView.Item("CONTACT_PERSON", Row)).Value)) ', "CONTACT_NUMBER")
Sw.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------")
Next
'MessageBox.Show(j)
'MessageBox.Show(i)
Sw.Close()
Fs.Close()
Fs1 = New FileStream("D:\MyFile1.txt", FileMode.Open)
Sr1 = New StreamReader(Fs1)
txtDisplay.Text = Sr1.ReadToEnd
Sr1.Close()
Fs1.Close()
Catch ex As Exception
'MessageBox.Show(ex.Message)
Finally
Sw = Nothing
Fs = Nothing
End Try
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
streamToPrint = New StreamReader("D:\MyFile1.txt")
Try
printFont = New Font("Arial", 10)
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.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 Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
''' at the very top of the form
Imports System.Drawing.Printing
''' new sub procedure
Private Sub printtext(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
ev.Graphics.DrawString(lblQ2.Text, New Font("arial", 11, FontStyle.Regular), Brushes.Black, 120, 120)
ev.HasMorePages = False
End Sub
''' button event
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Try
Dim printdoc As New PrintDocument
AddHandler printdoc.PrintPage, AddressOf Me.printtext
printdoc.Print()
Catch ex As Exception
MessageBox.Show("Sorry there is a problem printing", ex.ToString())
End Try
End Sub
Imports System.Drawing.Printing
''' new sub procedure
Private Sub printtext(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
ev.Graphics.DrawString(lblQ2.Text, New Font("arial", 11, FontStyle.Regular), Brushes.Black, 120, 120)
ev.HasMorePages = False
End Sub
''' button event
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Try
Dim printdoc As New PrintDocument
AddHandler printdoc.PrintPage, AddressOf Me.printtext
printdoc.Print()
Catch ex As Exception
MessageBox.Show("Sorry there is a problem printing", ex.ToString())
End Try
End Sub
_______
|} {} gg|
----------
|} {} gg|
----------
![]() |
Similar Threads
- Batch print barcode in ASP.NET, C#, on client side (ASP.NET)
- draft print in asp.net 2.0 (ASP.NET)
- How to print the items in a datagrid in ASP .Net (ASP.NET)
- Print a web page in ASP.net (ASP.NET)
- Printing using Web Control Print button VB.NET (JavaScript / DHTML / AJAX)
- Silent Print a PDF from VB.NET (VB.NET)
- Taking cmmd prompt data into Vb (Visual Basic 4 / 5 / 6)
- Problem printing from IE6 (Web Browsers)
Other Threads in the VB.NET Forum
- Previous Thread: Printing of a Form in VB.NET?
- Next Thread: Programming Tic Tac Toe in VB.NET
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account application arithmetic array basic bing button buttons center check code combobox component crystalreport data database datagrid datagridview date design dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy hosting images input insert intel internet listview mobile monitor ms net networking objects output panel passingparameters peertopeervideostreaming picturebox picturebox1 port position print printing problem problemwithinstallation project remove save searchbox searchvb.net select serial shutdown soap survey table tcp temperature text textbox timer timespan toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web winforms wpf year





