Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
why this code with Button_Click(On Form) excecute correctly,
But when it with Form_load Excecuted it not response???

private void button1_Click(object sender, EventArgs e)
       {
           String drawString = "Sample Text";

           // Create font and brush.
           Font drawFont = new Font("Arial", 16);
           SolidBrush drawBrush = new SolidBrush(Color.Black);

           // Create point for upper-left corner of drawing.
           Point drawPoint = new Point(20, 20);

           // Draw string to screen.
           Graphics p= pictureBox1.CreateGraphics();

           Graphics k = this.CreateGraphics();
           p.DrawString(drawString, drawFont, drawBrush, drawPoint);


       }


------------------------------------------------------------------------

private Form_Load(object sender, EventArgs e)       //not response
       {
           String drawString = "Sample Text";

           // Create font and brush.
           Font drawFont = new Font("Arial", 16);
           SolidBrush drawBrush = new SolidBrush(Color.Black);

           // Create point for upper-left corner of drawing.
           Point drawPoint = new Point(20, 20);

           // Draw string to screen.
           Graphics p= pictureBox1.CreateGraphics();

           Graphics k = this.CreateGraphics();
           p.DrawString(drawString, drawFont, drawBrush, drawPoint);


       }
Posted
Updated 22-Feb-11 9:04am
v3
Comments
Yusuf 22-Feb-11 15:05pm    
Let us paint the pain :-) updated the subject

The OnPain() method is only found in the Duke Nukem API.

(Sorry I couldn't resist making the joke.)

Seriously, though, why don't you just display a Label control instead of going through all that manual drawing stuff?

Maybe it will render as expected if you handle the form's Shown event instead. I believe this event is fired after the form has been rendered.
 
Share this answer
 
v4
Comments
Albin Abel 22-Feb-11 15:11pm    
i like this joke
Sergey Alexandrovich Kryukov 22-Feb-11 15:18pm    
Sure, my 5. My answer is general, why not to do CreateGraphics, please see.
--SA
As a rule of thumb, do not use CreateGraphics.
Instead, override OnPaint or handle Paint event and use the Graphics instance from the event arguments parameter.
(I think, you already got this advise, why not following it.)

Do you think you drawing on button click work correctly? I doubt it. During run time, try to invalidate the picture. Just put another windows on top of it, then bring you form on top again. All you rendering made on the button click will disappear. If this is what you expected then all right, it's correct :) . Another problem is you don't dispose the instance of Graphics. You should have used using statement to create this object, so at the end of using block it would call IDisposable.Dispose automatically. It does not matter though, because what you really need is OnPaint or even handler on Paint. If you want some change on the button click, just change the data used in rendering inside Paint event and call Control.Invalidate of the control you're painting on. Use Invalidate with parameter (there are couple of overloads) to invalidate just part of the picture is you want.

And, of course, if can not possible work on event FormLoad. In a way, this a fake event. if you handle it, it will be called from the form constructor, so I never used to, just call my set-up method from the constructor directly.

The form is not even shown at that time, what rendering would you expect?!

—SA
 
Share this answer
 
v4
According to MSDN[^], the Form.Load event: Occurs before a form is displayed for the first time.
This means that at the time that Form_Load is fired, none of the Graphic elements of the form have been generated yet. There is nothing to get the Graphics of, or paint on, etc. There are no visual elements yet.
 
Share this answer
 

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