943,879 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 7770
  • C# RSS
Jan 11th, 2006
0

Calling a class upon button click

Expand Post »
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);
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 11th, 2006
0

Re: Calling a class upon button click

take out that sender param in the draw line you do not need that and pass the graphics(g) not the paint event args
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Jan 12th, 2006
0

Re: Calling a class upon button click

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006
Jan 12th, 2006
0

Re: Calling a class upon button click

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.
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 12th, 2006
0

Re: Calling a class upon button click

C# Syntax (Toggle Plain Text)
  1. private void DrawLinesPointF(Graphics g)//<--change this to accept graphics not args
  2. {
  3. // Create pen.
  4. Pen pen = new Pen(Color.Black, 3);
  5. // Create array of points that define lines to draw.
  6. PointF[] points =
  7. {
  8. new PointF( 10.0F, 10.0F),
  9. new PointF( 10.0F, 100.0F),
  10. new PointF(200.0F, 50.0F),
  11. new PointF(250.0F, 300.0F)
  12. };
  13. //Draw lines to screen.
  14. g.DrawLines(pen, points);///<- use the passed graphics to draw
  15. }
  16.  
  17. private void button1_Click(object sender, System.EventArgs e)
  18. {
  19. Graphics g = button1.CreateGraphics(); //<--creates graphics to draw on button
  20.  
  21. // Creates a pen that draws in red.
  22. Pen myPen1 = new Pen(Color.Red, 3);
  23.  
  24. g.DrawLine(myPen1, 1, 1, 200, 200);
  25.  
  26. DrawLinesPointF(g);//<--passes button graphics
  27. }
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Jan 12th, 2006
0

Re: Calling a class upon button click

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
Reputation Points: 26
Solved Threads: 11
Posting Whiz in Training
f1 fan is offline Offline
275 posts
since Jan 2006
Jan 12th, 2006
0

Re: Calling a class upon button click

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
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Jan 13th, 2006
0

Re: Calling a class upon button click

Wow U All are AMAZING PPL!
Thanks a BILLiON! ^^
Reputation Points: 10
Solved Threads: 0
Light Poster
daidaiboyboy is offline Offline
26 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Converting a long to an Enum Value
Next Thread in C# Forum Timeline: Newbie question:Unable to pass reference to an object





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC