User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 391,899 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,558 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser:
Views: 3172 | Replies: 7
Reply
Join Date: Jan 2006
Posts: 26
Reputation: daidaiboyboy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
daidaiboyboy daidaiboyboy is offline Offline
Light Poster

Calling a class upon button click

  #1  
Jan 11th, 2006
Hi, all...

I wish to call upon the DrawLinesPointF class to draw an array of points for me after I click on a button with design name button1. Upon compilation of my code, there is no error. However, when executed, and I clicked on the button, the following exception error occured:

An unhandled exception of type 'System.InvalidCastException' occurred in DrawingLines_Testing.exe

Additional information: Specified cast is not valid.

And the following line was highlighted:

DrawLinesPointF(sender, (PaintEventArgs) e);

Below is part of my code. Is there any problems with my definitions of the classes? Why do I have the problem with calling the class DrawLinesPointF? What do I have or lack?

private void DrawLinesPointF(object sender, PaintEventArgs e)
{
// Create pen.
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
PointF[] points =
{
new PointF( 10.0F, 10.0F),
new PointF( 10.0F, 100.0F),
new PointF(200.0F, 50.0F),
new PointF(250.0F, 300.0F)
};
//Draw lines to screen.
e.Graphics.DrawLines(pen, points);
}

private void button1_Click(object sender, System.EventArgs e)
{
Graphics g = button1.CreateGraphics();

// Creates a pen that draws in red.
Pen myPen1 = new Pen(Color.Red, 3);

g.DrawLine(myPen1, 1, 1, 200, 200);

DrawLinesPointF(sender, (PaintEventArgs) e);
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Calling a class upon button click

  #2  
Jan 11th, 2006
take out that sender param in the draw line you do not need that and pass the graphics(g) not the paint event args
Reply With Quote  
Join Date: Jan 2006
Posts: 26
Reputation: daidaiboyboy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
daidaiboyboy daidaiboyboy is offline Offline
Light Poster

Re: Calling a class upon button click

  #3  
Jan 11th, 2006
Sorry... I don't get you... u mean change the last line to the following?

From : DrawLinesPointF(sender, (PaintEventArgs) e);

To : DrawLinesPointF(g);

Is this what u mean?

Anyway I tried this, but upon compilation, the following error comes out.

No overload for method 'DrawLinesPointF' takes '1' arguments

Please help... Thanks...
Reply With Quote  
Join Date: Jan 2006
Location: Its the internet... i am everywhere lol
Posts: 274
Reputation: f1 fan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Calling a class upon button click

  #4  
Jan 12th, 2006
Where to start.. your logic is wrong i suspect. Your code will draw it in the button.
First problem is your cast error you mention...
DrawLinesPointF(sender, (PaintEventArgs) e);

you are trying to cast e to PaintEventArgs but e is a SystemEventArgs which is empty so there is no way it will ever cast.

Secondly you are sending the button as the sender to your DrawLinesPointF function and in that function you get the graphics object to draw to, hence you will draw in the button and not on your form.

If you tell us what you are trying to achieve and on what then we can help, but rethink your logic first.
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Calling a class upon button click

  #5  
Jan 12th, 2006
private void DrawLinesPointF(Graphics g)//<--change this to accept graphics not args
{
// Create pen.
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
PointF[] points =
{
new PointF( 10.0F, 10.0F),
new PointF( 10.0F, 100.0F),
new PointF(200.0F, 50.0F),
new PointF(250.0F, 300.0F)
};
//Draw lines to screen.
g.DrawLines(pen, points);///<- use the passed graphics to draw
}

private void button1_Click(object sender, System.EventArgs e)
{
Graphics g = button1.CreateGraphics(); //<--creates graphics to draw on button

// Creates a pen that draws in red.
Pen myPen1 = new Pen(Color.Red, 3);

g.DrawLine(myPen1, 1, 1, 200, 200);

DrawLinesPointF(g);//<--passes button graphics
} 
Reply With Quote  
Join Date: Jan 2006
Location: Its the internet... i am everywhere lol
Posts: 274
Reputation: f1 fan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 10
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Calling a class upon button click

  #6  
Jan 12th, 2006
I dont think he wants to draw to the button though, which is why i asked. hopefully he will know how to get the graphics of whatever control he wants to draw to from your code
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Calling a class upon button click

  #7  
Jan 12th, 2006
oh well in that case jsut replace the "button1.CreateGraphics();"
with "Graphics g = this.CreateGraphics();"

that will draw on the form

you can draw on anything this way
Reply With Quote  
Join Date: Jan 2006
Posts: 26
Reputation: daidaiboyboy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
daidaiboyboy daidaiboyboy is offline Offline
Light Poster

Re: Calling a class upon button click

  #8  
Jan 12th, 2006
Wow U All are AMAZING PPL!
Thanks a BILLiON! ^^
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C# Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 7:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC