| | |
Calling a class upon button click
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
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);
}
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);
}
•
•
Join Date: Jan 2006
Posts: 26
Reputation:
Solved Threads: 0
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...
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...
•
•
Join Date: Jan 2006
Posts: 275
Reputation:
Solved Threads: 11
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.
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.
C# Syntax (Toggle Plain Text)
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 }
![]() |
Similar Threads
- Passing value of string from one button click event to another (ASP.NET)
- Run msn messenger on button click in asp.net 2.0 (ASP.NET)
- Run messenger on button click (ASP.NET)
- Button Click (ASP.NET)
- Tk Button Click Response (Python)
- Calling button-click events from different classes (Python)
Other Threads in the C# Forum
- Previous Thread: Copy string into an array
- Next Thread: Newbie question:Unable to pass reference to an object
| Thread Tools | Search this Thread |
.net 2007 access ado.net algorithm array barchart bitmap box broadcast buttons c# camera check checkbox client color combobox control conversion cs4 csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ httpwebrequest image index input install ip java label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox security serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml





