How to Call Paint Event

How do I call paint event?

The Invalidate() Method will cause a repaint.

MSDN Link

How to call paint event from a button click event?

This should do the trick:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}

private void button1_Click(object sender, EventArgs e)
{
using (var g = Graphics.FromImage(pictureBox1.Image))
{
g.DrawEllipse(Pens.Blue, 10, 10, 100, 100);
pictureBox1.Refresh();
}
}
}

How to call a Paint method from a Click event?

Case One: Drawing onto Controls:

Provided the Paint event is actually hooked up and not just a piece of code you have copied, this will do the job:

public void button1_Click(object sender, EventArgs e)
{
CrochetPtrnDesign.Invalidate();
}

Update: since we now know that CrochetPtrnDesign is the Form simply write: this.Invalidate();!

If in doubt about hooking up read this!

Note that creating truely valid PaintEventArgs should only be done by the system..

Case Two: Drawing into Bitmaps:

To draw into a Bitmap you also need a valid Graphics object, but you have to create it yourself:

void DrawStuff(Bitmap bmp)
{
using (Graphics g = Graphics.FromImage(bmp))
{
// draw your stuff
g.DrawRectangle(...);
}
// when done maybe assign it back into a Control..
pictureBox.Image = bmp;
}

Update:

Assigning the bitmap to the pictureBox.Image will work but runs the risk of leaking the previous image. To avoid this you can use a faile-safe method, maybe like this:

void SetImage(PictureBox pb, Bitmap bmp)
{
if (pb.Image != null)
{
Bitmap tmp = (Bitmap)pb.Image;
pb.Image = null;
tmp.Dispose();
}
pb.Image = bmp;

}

How to call Paint event on MouseHover?

Use Refresh() instead of Invalidate().

The Invalidate() method does not force repaint, it simply marks the control as "need repainting", leaving it up to the OS to repaint when needed.

The Refresh() method will force repainting immediately.

For more information, read Whats the difference between Control.Invalidate, Control.Update and Control.Refresh? blog post, where it clearly states:

Control.Invalidate( ) / Control.Invalidate(bool) / Control.Invalidate(Rectangle) / Control.Invalidate(Rectangle, bool) / Control.Invalidate(Region) / Control.Invalidate(Region, bool)
....
The important thing to note here is that these functions only “invalidate” or “dirty” the client area by adding it to the current update region of the window of the control. This invalidated region, along with all other areas in the update region, is marked for painting when the next WM_PAINT message is received. As a result you may not see your control refreshing (and showing the invalidation) immediately (or synchronously).

Update

In order for the panel to show the YellowGreen border only when the mouse is hovering over the button, you need to change your code a little bit.

First, add a bool field to your form, to indicate whether to draw the border or not, initialized to false:

private bool _drawBorder = false;

Then, change the paint method to take that value into consideration:

private void deal_details_panel_Paint(object sender, PaintEventArgs e)
{
if(_drawBorder)
{
int thickness = 2;//it's up to you
int halfThickness = thickness / 2;
using (Pen p = new Pen(Color.GreenYellow, thickness))
{
e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
halfThickness,
deal_details_panel.ClientSize.Width - thickness,
deal_details_panel.ClientSize.Height - thickness));
}
}
}

Finally, don't use the mouse_Hover event on the button, use MouseEnter and MouseLeave instead.

This is for two reasons - 1: Mouse_Hover have a built in delay and 2: using Mouse_Hover you don't get an indication when the mouse stops hovering over the button.

In the MouseEnter and MouseLeave event handlers, all you need to do is update the value of _drawBorder and call deal_details_panel.Refresh():

private void add_MouseEnter(object sender, EventArgs e)
{
_drawBorder = true;
deal_details_panel.Refresh();
}

private void add_MouseLeave(object sender, EventArgs e)
{
_drawBorder = false;
deal_details_panel.Refresh();
}

Calling paint event inside a function

You should save the coordinates to a field or list, what ever makes more sense for your case.

And then Invalidate the Panel, this will Raise the Paint Event, in which you will then be able to draw your line from the saved coordinates.

This is the only way to get what you most likely want without alot of trouble, because drawing to a window(Panel) in a non Standard way is not an easy Task to do.

[EDIT] Also:
Don't Forget you have to redraw the entire "Scene" on that Panel every time the Paint Event is raised, even if you use a Bitmap (backbuffer) as a backup, you need to blit this Bitmap onto the Panel again.

[EDIT] Sample:

    private readonly Stack<Point> _points = new Stack<Point>();
private readonly Pen _blackPen = new Pen(Color.Black);

private void Form1_Paint(object sender, PaintEventArgs e)
{
var points = _points.ToArray();
for (int i = 1; i < points.Length; i++)
{
e.Graphics.DrawLine(_blackPen, points[i - 1], points[i]);
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
_points.Push(e.Location);
Invalidate();
}

Calling a function that involves PaintEventArgs when a button is clicked

Typically in a Windows Forms application where you want to do custom drawing, you either draw directly on a Form or PictureBox in the Paint event handler or create a subclass of Control in which you override the OnPaint method. In the Paint event handler or OnPaint, you draw everything (i.e. not just one circle, but all the circles). Then when your underlying data structure changes, indicating that you need a repaint, you call Invalidate() on the control, which marks it as needing redraw, and on the next pass through the event loop, your Paint event handler will run or your OnPaint method will be called. Within that method, you'll have the PaintEventArgs object you need to get a Graphics object with which to do your drawing. You don't want to "draw once and forget" (e.g. when a button is clicked) because there are all sorts of things that can cause your control to need to repaint itself. See this question and answer for more details on that.

Edit: here's some hand-holding in response to your comment. (It was going to be a comment but it got too long.)

If I assume for the moment that you're starting with a blank Form in Visual Studio's Windows Forms Designer, the quickest way to get going would just be to select the Form, and in VS's Properties pane, click the lightning bolt toolbar button to view the form's events. Scroll down to the Paint event and double-click anywhere in the blank space to the right of the label. That will wire up a Paint event handler for the form and take you to the newly added method in the form's code file. Use the PaintEventArgs object called e to do your drawing. Then, if you need to change what gets drawn upon some button click, in your click handler change the data that determine what gets drawn (e.g. positions and colors of of the playing pieces) and, when you're done, call Invalidate().



Related Topics



Leave a reply



Submit