Solved

Printing causes page feed at each DrawString()

Posted on 2004-09-14
10
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0
 
LVL 1

Expert Comment

by:corpuslabs
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Author Comment

by:Clif
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
No.  You need to print all you are going to print on a page before calling EndDoc().
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now