2

I am assigning event handler to PrintPage as follows:

//pdPrint is PrintDocument object
pdPrint.PrintPage += new PrintPageEventHandler((sender, e) => PrintLabel(sender, e, request, orderdetail));

Once printing is done, I want to remove above event handler from pdPrint object, and then i want to assign it to new PrintPageEventHandler for printing another data.

Can we remove the PrintPageEventHandler from pdPrint object? I tried using following code, but its not working:

pdPrint.PrintPage -= new PrintPageEventHandler((sender, e) => PrintLabel(sender, e, request, orderdetail));

Can someone please tell me, how this can be done?

3
  • You cannot remove it, no way to get a reference to the exact same lambda expression. Use a plain event handler method instead. There is very little point in trying to preserve a PrintDocument object btw, just create a new one. Apr 18, 2016 at 16:07
  • Can you please tell me how to create a plain event method handler,as I want to pass other parameters like request and orderdetail to my event handler method? Apr 19, 2016 at 7:44
  • 1
    As I said, don't bother unsubscribing the event and simply create a new PrintDocument instance. Make it even better by deriving your own classes from PrintDocument and adding whatever fields you need. Pass their value in the constructor. Now it will also work correctly when you need to print multi-page documents. Apr 19, 2016 at 12:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.