Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Printing causes page feed at each DrawString()

Posted on 2004-09-14
10
301 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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