Click here to Skip to main content
15,439,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I make this work. I have two methods,

C#
public void Calculate(Graphics g)
 {
 ---code---
 }   

private void btnPrint_Click(object sender, EventArgs e)
 {
  ---code---
  Calculate(Graphics g);
 }   


I want to call the Calculate method inside the btnPrint_Click method but I cannot do it. Any help is appreciated.
Posted
Updated 1-Aug-12 12:06pm
v2
Comments
TRK3 1-Aug-12 18:11pm     CRLF
What do you mean "I cannot do it"? Do you mean the compiler complains because you should be calling: Calculate(g) instead of Calculate(Graphics g) ? Or do you mean you get an exception when you call it? Or ?
Kenneth Haugland 1-Aug-12 18:13pm    
How could I not see that... Post it as the answer....
Al Yambo 1-Aug-12 18:38pm     CRLF
I get an exception when I use either Calculate(g); , Calculate(Graphics g); inside of private void btnPrint_Click(object sender, EventArgs e) method. "The name g does not exist in the current context" & "System.Drawing.Graphics is a type but is used like a variable".

Easy. You would easily find a solution if you thought just a bit more.

C#
public partial class FormMain {
    
    void Print() {
        using (PrintDocument pd = new PrintDocument()) {
                if (PrinterSettings != null) 
                    PrintDialog.PrinterSettings = PrinterSettings;
                pd.PrintPage += (sender, eventArgs) => { this.Calculate(eventArgs.Graphics); };
                DialogResult result = PrintDialog.ShowDialog();
                if (result != DialogResult.OK) return;
                PrinterSettings = PrintDialog.PrinterSettings;
                PrintDialog.Document = pd;
                pd.PrinterSettings = PrinterSettings;
                // could be a lot more, like taking into account page setup, using page setup dialog...
                pd.Print();
            } //using (pd.Dispose is automatically called in this point)
    } //Print

    PrintDialog PrintDialog = new PrintDialog();
    PrinterSettings PrinterSettings;

    //...

} //class FormMain


Your Calculate(Graphics g) is actually a good idea, but you probably underestimate it. You abstract some rendering from concrete instance of System.Drawing.Graphics. This way, you can have only one graphics rendering method and call it from some controls OnPaint but also use it for other targets of rendering: "Print", "Print to…", "Export to bitmap", "Export to…" (some vector graphics) and the like — quite universal. Are you getting the idea?

—SA
 
Share this answer
 
v2
Comments
Al Yambo 1-Aug-12 19:10pm    
Thank you for your response. Let me digest your code a little bit and also try it in my application.
Sergey Alexandrovich Kryukov 1-Aug-12 19:19pm     CRLF
And then please accept the answer formally (green button). This is quite a working code, from my future article. --SA
Kenneth Haugland 1-Aug-12 19:35pm    
Gave you a 5!
Sergey Alexandrovich Kryukov 1-Aug-12 19:52pm     CRLF
Thank you, Kenneth. --SA
Totally rip off but I think TRK3 is right:
Quote:
What do you mean "I cannot do it"? Do you mean the compiler complains because you should be calling: Calculate(g) instead of Calculate(Graphics g) ? Or do you mean you get an exception when you call it? Or ?
 
Share this answer
 
Comments
Al Yambo 1-Aug-12 18:54pm     CRLF
I get an exception when I use either Calculate(g); , Calculate(Graphics g); inside of private void btnPrint_Click(object sender, EventArgs e) method. "The name g does not exist in the current context" & "System.Drawing.Graphics is a type but is used like a variable".
Sergey Alexandrovich Kryukov 1-Aug-12 19:00pm     CRLF
Of course... but... either think a bit more or see my solution. It's easy enough. (Double post due to some flaw on the server side... :-) --SA
Sergey Alexandrovich Kryukov 1-Aug-12 19:01pm     CRLF
Of course but... what are you doing? Just think a bit more... or see my solution. --SA
Sergey Alexandrovich Kryukov 1-Aug-12 18:59pm     CRLF
"I cannot do" is not informative, but in this particular case, it looks clear to me. Please see my answer. Actually, OP's Calculate(Graphics g) can be considered as a right idea, needs a bit more of elaboration... --SA
Kenneth Haugland 1-Aug-12 19:34pm    
Just copyed the whole response unedited, so everybody would know where i got i from.

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