Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Printing causes page feed at each DrawString()

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
Clif
Asked:
Clif
  • 5
  • 4
1 Solution
 
ClifAuthor Commented:
By the way, there is more to the TextPrint class, but what I posted is the only relavent sections (I think).
0
 
ClifAuthor Commented:
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
 
ee_ai_constructCommented:
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now