[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Printing causes page feed at each DrawString()

Posted on 2004-09-14
10
Medium Priority
?
315 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

656 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