Upgrading PrintForm Code

Upgrading PrintForm Code

Visual Basic 6 offers a simple way to print forms using the Form.PrintForm method. PrintForm is not perfect: some controls don t print well, and the quality is not always picture-perfect. But for many people, it s good enough. Unfortunately, Visual Basic .NET has no direct equivalent of PrintForm, and thus the Upgrade Wizard can t upgrade PrintForm code. Don t worry, though; we ve found a replacement for you, using a PrintForm helper class. (For the source code to this class, see the PrintForm application on the companion CD.) Here are the steps to implement PrintForm behavior in Windows Forms:

  1. Create a new Visual Basic .NET Windows application.

  2. Add the PrintForm helper class.

  3. Add a Button to the default form, and in the Button_Click event insert the following code:

    Dim c As New PrintForm() c.Print(Me)

  4. Run the application, and click the button. Bingo! Your form will be sent to the printer.

You should be aware of one limitation: the form to be printed must be the topmost form and must be entirely visible, since the class takes a snapshot of the form and prints it. In many cases this technique will be good enough, especially when you simply want to print the visible layout of the current form. As a bonus, the helper class also contains another function, getImageFromForm. This method copies the image of the form to an image object. You can use this to, say, copy the image from one form to another. Let s see how this works by extending the previous example:

  1. Add a second form, Form2, to your application.

  2. Add a second Button to Form1, and in the Button2_Click event insert the following code:

    Dim c As New PrintForm() Dim i As Image i = c.getImageFromForm(Me) Dim f2 As New Form2() f2.Show() f2.CreateGraphics.DrawImage(i, New Point(0, 0))

  3. Run the application, and click Button2. The application paints the image of Form1 onto Form2, as shown in Figure 15-8. The controls painted onto Form2 won t work, since we ve simply painted the form image. Nevertheless, it s a convincing illusion.

    Figure 15-8

    Which is the real Form1?

Where Are the Print Functions?

The printing model in the .NET Framework is quite different from that in Visual Basic 6. If you re looking for the printing functions, you ll find them in the System.Drawing.Printing namespace. For help with printing programmatically from your application, search for the topic Printing with the PrintDocument Component in the Visual Basic .NET Help system.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net