If I call a paint event through an event such as a button press, does said paint event need to be added to the properties of the relevant picturebox?

30 views Asked by At

I'm using C# visual studio, and I want to have a picture box redrawn every time a button is pressed. I have the appropriate calls that I got from other questions on this site.

Only problem is that if I don't put the paint event subroutine into the "paint" section on the picture box's properties, the button press will do nothing. But if I do put that paint event in, the button will be ignored and the picture box will paint itself automatically.

So what do I need to do?

1

There are 1 answers

0
JonasH On

You should probably call either Invalidate or Refresh when something need to be redrawn. But painting is managed by the framework, you can trigger it explicitly, but it will also trigger in all kinds of circumstances outside of your control.

Note that PictureBox is for displaying pictures, you would typically "update" it by replacing its Image-property, and that should to the invalidation of the control for you. So, if you want to show a picture when the button is pressed, just set the image property in the button event handler.

If you want to do some kind of more fancy drawing you probably want to create a custom control and override OnPaint. If you want to paint the control in some special way you could set some flag in your button event handler, like

// In Button event handler:
    myCustomControl.PaintSpecial = true;
    myCustomControl.Invalidate();

// In MyCustomControl
public bool PaintSpecial {get;set;}
public override OnPaint(System.Windows.Forms.PaintEventArgs e){
    if(PaintSpecial){
       // do special painting
       return;
    }
    base.OnPaint(e);
}