Can I convert EventArgs type to PaintEventArgs type ?
-
I want to convert EventArgs type to PaintEventArgs type, cast button_click to form_paint, how do I write code ? You see my error code
namespace DrawText_Random
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//progressBar1.Visible = false;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form9\_Paint); } int valueforShadowedTextPaint = 0; private void btnDrawText\_Click(object sender, EventArgs e) //PaintEventArgs e { progressBar1.Value = 0; this.timer1.Interval = 100; this.timer1.Enabled = true; } private void ShadowedTextPaint(PaintEventArgs e, int num) { // Set up the font Font fnt = new Font("Calibri", 296, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point); // Get the Form's Graphics object Graphics g = e.Graphics; // Draw background text g.DrawString(num.ToString(), fnt, Brushes.DarkGray, 244, 104); // Draw main text slightly offset from shadow text g.DrawString(num.ToString(), fnt, Brushes.Aqua, 240, 100); } private void timer1\_Tick(object sender, EventArgs e) { if (progressBar1.Value < 100) { PaintEventArgs g = (PaintEventArgs)e;//Error here: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.PaintEventArgs'. Random rd = new Random(); Random rd = new Random(); int Number = rd.Next(0, 999); //This place must call ShadowedTextPaint (...) function to jump continuously ShadowedTextPaint(e, Number); progressBar1.Value++; } else { this.Invalidate(); this.timer1.Enabled = false; } } }
}
-
I want to convert EventArgs type to PaintEventArgs type, cast button_click to form_paint, how do I write code ? You see my error code
namespace DrawText_Random
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//progressBar1.Visible = false;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form9\_Paint); } int valueforShadowedTextPaint = 0; private void btnDrawText\_Click(object sender, EventArgs e) //PaintEventArgs e { progressBar1.Value = 0; this.timer1.Interval = 100; this.timer1.Enabled = true; } private void ShadowedTextPaint(PaintEventArgs e, int num) { // Set up the font Font fnt = new Font("Calibri", 296, FontStyle.Bold | FontStyle.Italic, GraphicsUnit.Point); // Get the Form's Graphics object Graphics g = e.Graphics; // Draw background text g.DrawString(num.ToString(), fnt, Brushes.DarkGray, 244, 104); // Draw main text slightly offset from shadow text g.DrawString(num.ToString(), fnt, Brushes.Aqua, 240, 100); } private void timer1\_Tick(object sender, EventArgs e) { if (progressBar1.Value < 100) { PaintEventArgs g = (PaintEventArgs)e;//Error here: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.PaintEventArgs'. Random rd = new Random(); Random rd = new Random(); int Number = rd.Next(0, 999); //This place must call ShadowedTextPaint (...) function to jump continuously ShadowedTextPaint(e, Number); progressBar1.Value++; } else { this.Invalidate(); this.timer1.Enabled = false; } } }
}
No, you can't. Think about it this way: You have a car, and it's a Ford Fiesta. You take a badge off a Ferrari, and glue it on in place of the Ford oval badge. Does your car now do 200 mph and go round corners really well? Is it suddenly worth £100,000? Of course not - and you are trying to do much the same thing with your code. When you derive a class B from class A, B gets everything that is in A : field, properties, methods, events. But you have added things to B - other fields, other methods, other properties, and these do not exist in A. If you could cast an instance of A to an instance of B what would happen when you tried to use the new properties and methods? They don't exist in the original, so any information you tried to use would be "in limbo" and your app would crash because the system can't just "invent" the missing data! Instead, you can create a new PaintEventArgs, and supply that with a new Graphics context to draw on then pass the new instance to your ShadowedTextPaint method instead - remembering to dispose of the Graphics context when you are finished with it. You can get the context with the Control.CreateGraphics Method[^] But me? I'd pass just the Graphics context instead of a whole "dummy" PaintEventArgs - it's the only bit you need, anyway!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
No, you can't. Think about it this way: You have a car, and it's a Ford Fiesta. You take a badge off a Ferrari, and glue it on in place of the Ford oval badge. Does your car now do 200 mph and go round corners really well? Is it suddenly worth £100,000? Of course not - and you are trying to do much the same thing with your code. When you derive a class B from class A, B gets everything that is in A : field, properties, methods, events. But you have added things to B - other fields, other methods, other properties, and these do not exist in A. If you could cast an instance of A to an instance of B what would happen when you tried to use the new properties and methods? They don't exist in the original, so any information you tried to use would be "in limbo" and your app would crash because the system can't just "invent" the missing data! Instead, you can create a new PaintEventArgs, and supply that with a new Graphics context to draw on then pass the new instance to your ShadowedTextPaint method instead - remembering to dispose of the Graphics context when you are finished with it. You can get the context with the Control.CreateGraphics Method[^] But me? I'd pass just the Graphics context instead of a whole "dummy" PaintEventArgs - it's the only bit you need, anyway!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
I understand you say, I mean how to remove the Form2_Paint method and you understand I say, you instructed me to use the new class by declaring Graphics g = this.CreateGraphics(), after creating a new class My program has run, but I have a problem with random numbers that it overwrites continuously on the screen, I want to draw a new number, I have to restore the screen before drawing, do you have any way ? My program corrects the code as follows:
...
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 100)
{
...
ShadowedTextPaint(Number);
...
}
else
{
...
}
}private void ShadowedTextPaint(int num)
{
...
Graphics g = this.CreateGraphics();//e.Graphics;
...
}
... -
I understand you say, I mean how to remove the Form2_Paint method and you understand I say, you instructed me to use the new class by declaring Graphics g = this.CreateGraphics(), after creating a new class My program has run, but I have a problem with random numbers that it overwrites continuously on the screen, I want to draw a new number, I have to restore the screen before drawing, do you have any way ? My program corrects the code as follows:
...
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 100)
{
...
ShadowedTextPaint(Number);
...
}
else
{
...
}
}private void ShadowedTextPaint(int num)
{
...
Graphics g = this.CreateGraphics();//e.Graphics;
...
}
...First off, as I mentioned, if you create a Graphics object, you are responsible for Disposing it - or you app (and possibly the whole of Windows) will crash when it runs out of a very limited resource! The best way to handle that is to use a
using
block:private void ShadowedTextPaint(int num)
{
...
using (Graphics g = this.CreateGraphics())
{
...
}
}And the system will Dispose the Graphics when it goes out of scope. But ... why are you messing about with this? Add a Panel or similar control to your form as a place to write your number, and in yoru Tick event handler just call Invalidate on the Panel. Add a Panel.Paint event handler and draw your text on the panel in that using the Graphics object it supplies. That way, it all fits neatly into the "normal processing" of the form, and you don't have to mess about so much - or worry about everything else that is going on.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
First off, as I mentioned, if you create a Graphics object, you are responsible for Disposing it - or you app (and possibly the whole of Windows) will crash when it runs out of a very limited resource! The best way to handle that is to use a
using
block:private void ShadowedTextPaint(int num)
{
...
using (Graphics g = this.CreateGraphics())
{
...
}
}And the system will Dispose the Graphics when it goes out of scope. But ... why are you messing about with this? Add a Panel or similar control to your form as a place to write your number, and in yoru Tick event handler just call Invalidate on the Panel. Add a Panel.Paint event handler and draw your text on the panel in that using the Graphics object it supplies. That way, it all fits neatly into the "normal processing" of the form, and you don't have to mess about so much - or worry about everything else that is going on.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
I am just beginning to learn about graphical functions, I do not know much about them, so I will listen and absorb your comments on the forum. I want to write a sequential random number jump program that uses graphical functions, if I use the Form1_Paint method it doesn't allow the random number jump continuously at will because my timer uses timer1_Tick, Currently I have not thought of how to use the Form1_Paint method to jump random numbers continuously, according to your instructions I will use the using (...) {...} block to write the program, I have used using block already but do not know how to prevent overwriting, you see the image file is overwritten, do you have any way ? http://www.mediafire.com/view/behinj51ttc7ak5/ShadowTextRandom01.jpg/file http://www.mediafire.com/view/0u5gqhymhxp6ygb/ShadowTextRandom02.jpg/file