Solved

Printing causes page feed at each DrawString()

Posted on 2004-09-14
10
300 Views
Last Modified: 2008-02-01
Leave it to Microsoft to take a nice simple concept like the Printer object and turn it into something so convuluted it's a pain to use or even figure out how to use.

Now that rant is over, on to my problem/question.

I've been working on a class to emulate the old VB6 Printer object.  It's working well so far except every time I print a line, I get a page feed.

The trimmed sections of my class are as follows:

Public Class TextPrint
    ' Inherits all the functionality of a PrintDocument
    Inherits Printing.PrintDocument
    ' Private variables to hold default font and text
    Private m_fntPrintFont As Font
    Private m_sText As String
    Private m_nCurrentX As Integer
    Private m_nCurrentY As Integer

    Public Sub New()
         MyBase.New()
    End Sub

    Public Shadows Sub [Print](ByVal sText As String)
        m_sText = sText
        MyBase.Print()
    End Sub

    Protected Overrides Sub OnBeginPrint(ByVal ev As Printing.PrintEventArgs)
        ' Run base code
        MyBase.OnBeginPrint(ev)
        ' Sets the default font
        If m_fntPrintFont Is Nothing Then
            m_fntPrintFont = New Font("Times New Roman", 12)
        End If
    End Sub

    Protected Overrides Sub OnPrintPage(ByVal ev As Printing.PrintPageEventArgs)
        ' Provides the print logic for our document

        ' Run base code
        MyBase.OnPrintPage(ev)
        ev.Graphics.DrawString(m_sText, m_fntPrintFont, Brushes.Black, m_nCurrentX, m_nCurrentY)
        ev.HasMorePages = False
    End Sub
End Class

The code that calls the class is as follows:
    Dim Printer As New TextPrint
    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
        Printer.Print("This is line 1")
        Printer.Print("This is line 2")
    End Sub

Two questions in one...

1. How do I stop the printer from page feeding between "This is line 1" and "This is line 2"?

2. Once I stop if from page feeding when I don't want it, how to I force a page feed (ala EndDoc)?
0
Comment
Question by:Clif
  • 5
  • 4
10 Comments
 
LVL 10

Author Comment

by:Clif
ID: 12054934
By the way, there is more to the TextPrint class, but what I posted is the only relavent sections (I think).
0
 
LVL 10

Author Comment

by:Clif
ID: 12057766
I solved the question(s) myself.

Basically I discovered that it was the function of OnPagePrint to print a page and then page feed it.

What I ended up doing was creating a textout buffer (an array of a structure) and loadingit with each call to Print().  Then my EndDoc() would call the MyBase.Print() routine.

The class is modified as follows:
Public Class TextPrint
'
'
'
    Private Structure strOutput
        Dim TextOut As String
        Dim fnt As Font
        Dim CurrentX As Integer
        Dim CurrentY As Integer
    End Structure
    Dim Output() As strOutput
'
'
'
    Public Sub New()
        ' Sets the file stream
        MyBase.New()
        ReDim Output(0)
    End Sub
'
'
'
    Public Shadows Sub [Print](ByVal sText As String)
        Dim i As Integer
        Dim mg As Graphics = MyBase.PrinterSettings.CreateMeasurementGraphics()
        i = Output.Length
        ReDim Preserve Output(i)
        With Output(i)
            .TextOut = sText
            .fnt = m_fntPrintFont
            .CurrentX = m_nCurrentX
            .CurrentY = m_nCurrentY
            Dim szF As System.Drawing.SizeF = mg.MeasureString(sText, m_fntPrintFont)
            m_nCurrentX = 0
            m_nCurrentY += szF.Height
        End With
    End Sub
    Public Sub EndDoc()
        MyBase.Print()
    End Sub
'
'
'
    Protected Overrides Sub OnPrintPage(ByVal ev As Printing.PrintPageEventArgs)
        ' Provides the print logic for our document

        ' Run base code
        MyBase.OnPrintPage(ev)
        For i As Integer = 1 To Output.Length - 1
            With Output(i)
                ev.Graphics.DrawString(.TextOut, .fnt, Brushes.Black, .CurrentX, .CurrentY)
            End With
        Next
        ReDim Output(0)
        ev.HasMorePages = False
    End Sub
'
'
'
End Class
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12104386
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Expert Comment

by:corpuslabs
ID: 12223997
when i use the above code, my file is sent to the printer but the printer is not printing and the status of the document remains as "printing". what do i do ?

thanking u


anto
0
 
LVL 1

Expert Comment

by:corpuslabs
ID: 12224818
this is anto here, sorry for my previous question clif, there was some problem with my printer.

i am now stuck at this problem. when i execute the code u have given here, the onprintpage code is never executed at all, same is the case with the onbeginprint sub also. execution happens in the following area only

Public Shadows Sub [Print](ByVal sText As String)
        Dim i As Integer
        Dim mg As Graphics = MyBase.PrinterSettings.CreateMeasurementGraphics()
        i = Output.Length
        ReDim Preserve Output(i)
        With Output(i)
            .TextOut = sText
            .fnt = m_fntPrintFont
            .CurrentX = m_nCurrentX
            .CurrentY = m_nCurrentY
            Dim szF As System.Drawing.SizeF = mg.MeasureString(sText, m_fntPrintFont)
            m_nCurrentX = 0
            m_nCurrentY += szF.Height
        End With

    End Sub

and i get an error cause the font is not set as a result of the onbeginprint not being called...

can u help me ? i am stuck at this for a long long time

thanking u

anto
0
 
LVL 10

Author Comment

by:Clif
ID: 12225462
Answer to first question.
Sorry about that.

I forgot a line of code.

the click even should look like this:

    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
        Printer.Print("This is line 1")
        Printer.Print("This is line 2")
        Printer.EndDoc()    '<-- I forgot this line!!!
    End Sub

----------
Answer to second question.
You need to set the font before calling the Print() method.  You could, if you want, set a default font (m_fntPrintFont) in the class's constructor.  I didn't because I'm always setting the font before I call Print()
0
 
LVL 1

Expert Comment

by:corpuslabs
ID: 12245560
hi clif,

when i execute the code u have given , my application does include page feed. onprintpage is not called at all. after printing the text, the page feed happens and then the next page is made ready. how i do invoke onprintpage ?

thanking u,

anto
0
 
LVL 10

Author Comment

by:Clif
ID: 12247343
The EndDoc() method in the class calls MyBase.Print() which should cause the OnPrintPage() event to be fired.

    Public Sub EndDoc()
        MyBase.Print()
    End Sub
0
 
LVL 1

Expert Comment

by:corpuslabs
ID: 12248559
clif,

thanks for the reply. onprintpage does get called but after printing the page scrolls to the end of the page. i want the cursor to stop where it finished printing.. is this possible ?
0
 
LVL 10

Author Comment

by:Clif
ID: 12248623
No.  You need to print all you are going to print on a page before calling EndDoc().
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question