Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing formatted text in vb.net

Status
Not open for further replies.

irocz

IS-IT--Management
Oct 28, 2001
25
US
HI,

I use this line in VB6 to print columns in a table, when I converted to vb.net, I cannot find the equivalent. I went through documentation, examples, this forum, FAQ and search, but I cannot find even a clue, I am trying to print one line at a time retreiving fields from a table:
"
Printer.Print Tab(2); !crn; Tab(14); !priort; Tab(28); Format(!CDate, "mm/dd/yyyy"); Tab(42); Trim(!projnm); Tab(77); Trim(!subby); Tab(107); Trim(!dept); Tab(AlignDec(155, !totcst)); Format(!totcst, "###,###,##0.00"); Tab(163); Trim(IIf(IsNull(!assto), "Not Assigned ", !assto))"

Using the PrintDocument class and/or the Graphics class can show you how to print one string or one page but not the above.
Can anyone help?, please
 
So you want to send this line to the printer and let it print it and then wait for the next line to print (like on a dotmatrix printer with a continious feed of paper)?

So what printer are you using.

And what code do you have so far?



Christiaan Baes
Belgium

"My new site" - Me
 
Thanks Christiaan,

I am trying to print the content of the table, formatted, as shown, the printer is an hp laser, but since it is on lpt1, I use it as the default printer, since the user might have a different printer, and I do not use the printer dialog subclass:
***

Private Sub pacr_Click()
Dim hdtext As String
Dim nopcl As String
Dim nCnt As Long
hdtext = "All Closed Change Requests"
nopcl = "Closed by"
nCnt = 0
nPage = 0
nLine = 1
Printer.Orientation = 2


cc_headings hdtext, nopcl
Set rs = db.OpenRecordset("select * from ccmstr order by crn")


With rs
.MoveFirst

Do While Not .EOF
If nLine > 50 Then
cc_headings hdtext, nopcl
End If
If !crstat = "C" Then
Printer.Print Tab(2); !crn; Tab(14); !priort; Tab(28); Format(!CDate, "mm/dd/yyyy"); Tab(42); Trim(!projnm); Tab(77); Trim(!subby); Tab(107); Trim(!dept); Tab(AlignDec(155, !totcst)); Format(!totcst, "###,###,##0.00"); Tab(163); Trim(!clsdby)
nCnt = nCnt + 1
nLine = nLine + 1
End If
.MoveNext

Loop

rs.Close

End With

Printer.FontBold = True
Printer.Print

Printer.Print Tab(72); "Total Closed Change Requests : "; Tab(112); nCnt

Printer.Print
Printer.Print Tab(72); "*** End of Report ***"
Printer.FontSize = nFont
Printer.EndDoc
MsgBox "Printing is now completed", vbInformation
End Sub

Edward
 
Do you want me to change the code to vb.net? Or do you want someone else to help you.

Christiaan Baes
Belgium

"My new site" - Me
 
Hi Christaan,

Thanks again. If you can change the code to produce the same result I would appreciate it, since I can't find any other responses.

Edward
 
So here I go. This code will need some work to do what you want.

Code:
Dim objPrintdialog As New PrintDialog
    Dim WithEvents objPrintDocument As New Printing.PrintDocument
    Dim rs As adodb.recordset
    Dim db As adodb.database

    Public Sub Print()
        'Adapt to choose the printer you want or ommit to print on the default printer
        Dim printername As String = "\\server\printername"
        For inttemp As Integer = 1 To Printing.PrinterSettings.InstalledPrinters.Count
            If System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(inttemp - 1).ToString.ToUpper = printername.ToUpper Then
                objPrintDocument.PrinterSettings.PrinterName = System.Drawing.Printing.PrinterSettings.InstalledPrinters.Item(inttemp - 1).ToString
            End If
        Next
        rs = db.OpenRecordset("select * from ccmstr order by crn")
        rs.MoveFirst()
        objPrintDocument.Print()
    End Sub

    Private Sub objPrintDocument_PrintPage(ByVal sender As Object, ByVal e As Drawing.Printing.PrintPageEventArgs) Handles objPrintDocument.PrintPage
        Dim intlines As Integer = 0
        Dim y, x As Integer
        Dim g As Graphics
        Dim documentfont As New System.Drawing.Font("Arial", 8, FontStyle.Bold)
        Dim documentbrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black)

        Do While Not rs.EOF
            If intlines > 50 Then
                e.HasMorePages = True
            End If
            If rs!crstat = "C" Then
                g.DrawString(rs!crn, documentfont, documentbrush, 10, 20 * y)
                g.DrawString(rs!priort, documentfont, documentbrush, 30, 20 * y)
                g.DrawString(Format(rs!CDate, "mm/dd/yyyy"), documentfont, documentbrush, 10, 20 * y)
                g.DrawString(Trim(rs!projnm, documentfont, documentbrush, 50, 20 * y)
                g.DrawString(Trim(rs!subby), documentfont, documentbrush, 70, 20 * y)
                g.DrawString(Trim(rs!dept), documentfont, documentbrush, 90, 20 * y)
                g.DrawString(Trim(rs!clsdby), documentfont, documentbrush, 110, 20 * y)
                g.DrawString(Format(rs!totcst, "###,###,##0.00"), documentfont, documentbrush, 130, 20 * y)
                intlines += 1
            End If
            rs.MoveNext()
        Loop

        If rs.eof Then
            rs.Close()
            g.DrawString("Total Closed Change Requests : ", documentfont, documentbrush, 10, 20 * y)
            g.DrawString("***   End of Report   ***", documentfont, documentbrush, 10, 20 * y)
            MessageBox.Show("Printing is now completed", "Print", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
End Sub

Christiaan Baes
Belgium

"My new site" - Me
 
But I think you would be better of using a reportengine to do what you want, something like Crystal reports or sharpshooter from 9rays. Would do a better job.

And you should switch to ado.net.

Christiaan Baes
Belgium

"My new site" - Me
 
Thanks Christiann.

I do have Crystal Reports, and I thought about using it, but because of the approval workflow within the system and the other required functions, I wanted to house the reporting within it.

Thanks again.

Edward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top