Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to translate this snippet to C++/CLI from c#, but I'm not even sure what it is:

private PrintDocument document = new PrintDocument();
    document.PrintPage += new PrintPageEventHandler(PrintPage);


So far this is what I've made of it, but I think I need another parameter:
PrintDocument^ m_document;
m_document->PrintPage += gcnew PrintPageEventHandler(PrintPage);
Posted
Updated 6-Jun-14 11:21am
v2
Comments
Philippe Mori 6-Jun-14 19:55pm    
For event handler, you can look at compiler generated code to see the exact syntax or look at the documentation. In your example above, you have to call gcnew for the document too. And by the way, most of the time when you add event handler manually, it is better to remove them too.

I'm going try this on the basis that it compiles - the proof will come at run time!

m_document->PrintPage += gcnew PrintPageEventHandler(this, PrintPage);
 
Share this answer
 
This just declares a variable named 'document' of type PrintDocument, and initializes it with default constructor.
Then it sets up an event handler for its PrintPage event, which is a delegate pointing to a PrintPage() method that should be declared somewhere in the class.

How to wire up an event to a delegate method in C++/CLI is out of my knowledge, unfortunately. Sorry. I was not sure if you had problems with the C# or the C++/CLI part.
 
Share this answer
 
Comments
Ger Hayden 6-Jun-14 17:23pm    
Apologies - omitted the declaration from the OP, but I've added it now. The C# is from a working example, so it's good.
Change
C++
PrintDocument^ m_document;
m_document->PrintPage += gcnew PrintPageEventHandler(PrintPage);

to
C++
PrintDocument^ m_document;
m_document->PrintPage += gcnew PrintPageEventHandler(&PrintPage);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900