Print text to a form

IN Vb 6.0, using the Print command would print whatever text you put onto the form. Im wondering how you would do that same thing in VB2005 because apparently the command had changed...

thank you

[208 byte] By [Rekar] at [2008-2-17]
# 1

I don't believe that there is anything comparable without using GDI. The easiest thing to do is add a label to the form and write text into it.

Hope this helps.

DeborahK at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 2
The graphics classes drawstring method is the closest replacement
KenTucker at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 3
As you probably imagined, there's a ton of people with the same problem.
nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 4

Hi-

Relief is on the way! Stay tuned for an anouncement that is going to make life much better for PrintForm scenarios... very soon.

Best,

Paul Yuknewicz

Lead Program Manager
Visual Basic

PaulYuk_MS at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 5
Ehrm,

not to sound ungrateful but we've been hearing these "relief is on the

way" statements for a while. God knows how many threads I've

tried to answer asking about printing forms and enumerating

printers. Sounds like you've added some "everybody needs this

code" to SP1. Pray tell, when can we expect this, "summer 2006"

and "2006 Q3" is running out of summer and 'Q'. Looking at the

Product Feedback responses to filed bugs on the framework, you're not

contemplating an SP for .NET 2.0. Are you going to give us a new

assembly? Details and deadlines, please. People can wait a

couple of months, they can't wait half-a-year....

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 6

This particular person did not ask about printing a form, but rather how to print text to a form. I think that is a different question.

If the question *was* how to print a form, here is how I do it:

Private Sub ProcessPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)

Dim r As Rectangle

Dim frmToPrint As Form = Me.ActiveMdiChild

' Confirm that a form has been defined for printing

If frmToPrint Is Nothing Then

e.Cancel = True

Throw New InvalidOperationException("There is no active MDI child")

End If

' Define a (0,0) based rectangle for the image

r = New Rectangle(0, 0, frmToPrint.Width, frmToPrint.Height)

' Create the bitmap of the image

Dim b As New Bitmap(frmToPrint.Width, frmToPrint.Height)

frmToPrint.DrawToBitmap(b, r)

' Draw the bitmap to the printdocument scaled

' to the printer's margins, but keeping the

' same aspect ratio

Dim iNewHeight As Integer, iNewWidth As Integer

Dim dPrinterAspectRatio As Double = e.MarginBounds.Height / e.MarginBounds.Width

Dim dFormAspectRatio As Double = frmToPrint.Height / frmToPrint.Width

If dFormAspectRatio > dPrinterAspectRatio Then

' Form has bigger height, so set the height to max and scale

iNewHeight = e.MarginBounds.Height

iNewWidth = CType(e.MarginBounds.Height / dFormAspectRatio, Integer)

Else

' Form has a bigger width, so set the width to max and scale

iNewHeight = CType(e.MarginBounds.Width * dFormAspectRatio, Integer)

iNewWidth = e.MarginBounds.Width

End If

' Define a new rectangle for the scaled image

r = New Rectangle(e.MarginBounds.X, e.MarginBounds.Y, iNewWidth, iNewHeight)

e.Graphics.DrawImage(b, r)

End Sub

P.S. This works in VS 2005...

DeborahK at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 7

For those of you who were looking for a Form.Print or PrintForm feature, here it is!

http://msdn.microsoft.com/vbasic/downloads/powerpacks/default.aspx

PaulYuk_MS at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...
# 8

Hi DeborahK,

This posting was very helpful in trying to get a form scaled and printed. Thank you for the posting. I've come across one little difficulty with it that I hope you can assist me with and resolve. The first request goes thru very quickly. Subsequent prints take longer and longer.I have discovered by putting a Beep() statement and the beginning of the subroutine, the first it goes through the subroutine it beeps once, each time the subroutine is called again it beeps an additional time.So, by the time you’ve called the routine the tenth time you will hear 10 beeps.Is there something I need to clear or reset to eliminate this issue.I’ve also tried message boxes and see the same results.The first time ran you will the box once then an additional message box each time a print request is generated.

Thanks for the posting.I look forward to your response and solution.

New_to_Vb_studio at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Interop and Upgrade...