Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to draw lines on a picturebox object on my form by calling a paintevent as follow:


C#
private void button3_Click(object sender, EventArgs e)
{
      this.pictureBox1.Paint += new
      System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
     ......code...
}


but for some reason the paint event isn't fired, although I used simular code in other projects.

Can someone advise if I missed something?

What I have tried:

I have added a Breakpoint in the paint event, but in debug mode this breakpoint isn't hit.
I also put a MessageBox.Show event in the paint event, but with no result.
Using pictureBox1.Invalidate() didn't work either.

The above made me conclude that the paint event wasn't fired
Posted
Updated 1-Sep-21 2:43am

Why on earth are you wiring up the Paint event handler in a button Click handler? That should be wired up once, usually when the app starts.

You can then call your PictureBox.Invalidate method in the Click event handler to get the PictureBox to repaint.

Wiring up an event handler over and over again does nothing for you. It does NOT fire the Paint event.
 
Share this answer
 
Check that the button click event is fired: put a breakpoint there and check the handler is added (though adding a handler each time the button is clicked is a bad idea).

The most likely reason for a Paint handler not firing is it isn't hooked up, and that implies that the button event isn't hooked up either, or it's all hooked to the wrong instance of the form.

When you are sure it is attached, check that the PictureBox itself is Invalidated at some point - Paint is only raised when it's needed, it isn't on a timer or anything.

Quote:
Hi OriginalGriff, done it all. The button click is fired, but the paint event is not hit. Invalidate the picturebox doesn't help either.
I'm a bit lost here

All I can say is "it works fine for me":
C#
private void button1_Click(object sender, EventArgs e)
    {
    pictureBox1.Paint += new PaintEventHandler(PictureBox1_Paint);
    }

private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {
    e.Graphics.DrawRectangle(Pens.Green, new Rectangle(5, 5, pictureBox1.Width - 10, pictureBox1.Height - 10));
    }

private void button2_Click(object sender, EventArgs e)
    {
    pictureBox1.Invalidate();
    }
 
Share this answer
 
v2
Comments
Member 11336683 1-Sep-21 4:13am    
Hi OriginalGriff, done it all. The button click is fired, but the paint event is not hit. Invalidate the picturebox doesn't help either.
I'm a bit lost here
OriginalGriff 1-Sep-21 4:40am    
Answer updated.
Member 11336683 1-Sep-21 4:59am    
OK, strange.
By the way, when I add a refresh() after "pictureBox1.Paint += new PaintEventHandler(PictureBox1_Paint);" it works

Thx for your time and quick response
OriginalGriff 1-Sep-21 5:27am    
Refresh forces an Invalidate on the form and all child controls - which implies that your Invalidate isn't on the right PictureBox

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