Solved

VS2005: PrintDocument programmatorically add EventHandlers

Posted on 2006-07-20
2
308 Views
Last Modified: 2009-07-29
Hello fellow experts,

I'm trying to get some stuff printed; that's all not so difficult, well, you know, but I have this particular situation where I HAVE to programmatorically make the PrintDocument.

This ofcourse puts me in a difficult position; by programmatorically adding the PrintDocument, instead of during design, I also have to programmatorically assign the EventHandlers, and this is where things go awry.

This is the code:

    Private pd As New PrintDocument

        pd.PrinterSettings = New PrinterSettings()
        AddHandler pd.BeginPrint, PrintDocument1_BeginPrint(Me, New PrintEventArgs())
        Dim gr As Drawing.Graphics = Nothing
        AddHandler pd.PrintPage, PrintDocument1_PrintPage(Me, New PrintPageEventArgs(gr, New Drawing.Rectangle(), New Drawing.Rectangle(), New PageSettings()))


ok so first of, the AddHandler requires that I use FUNCTIONS as EventHandlers because it wants a return value; however, I don't want that (and it kinda fubars if it does). anyway, skipping the return value only gives me a warning if i use functions so ok, that's cool.

The second problem is, ofcourse, the EventArgs.
Adding Event Handlers in runtime requires you to add the right EventArgs aswell; I don't know these, so my code basically stops running on the AddHanlder pd.PrintPage line, giving me an error because gr is empty.

So, basically, what I need to know from the experts, is a way to add a printdocument and its eventhandlers (specifically the onBeginPrint and onPrintPage ones) during RUNTIME.

thanks in advance !

cheers,
Andy
0
Comment
Question by:AndyAelbrecht
2 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 500 total points
ID: 17147697
To get the correct event args  just create the events during design time and then remove the Handles clause.  Then when you call addhander during runtime you just do AddressOf the sub.

This would make your addHandler look like this

AddHandler pd.BeginPrint, AddressOf PrintDocument1_BeginPrint
0
 
LVL 4

Author Comment

by:AndyAelbrecht
ID: 17163420
*doh* :)

thx !
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

837 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