hi everyone, i'm new in c# and events handling techniques, and i have a problem:

how can i call paint event inside of mouseclick event?

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Form1_Paint(?, ?);  //i need something like this...
}

i want to move rectangle(paint event moves it, right?) to place where mouse is clicked on the form.

Dani AI

Generated

You generally should not call your Paint handler (or OnPaint) from MouseClick. Instead, store the new state in the click handler and ask WinForms to repaint. The framework will then raise Paint and give you a valid Graphics in PaintEventArgs. Use Invalidate() to request a repaint (or Invalidate(rect) to limit work to the changed areas). If you need the repaint immediately, follow with Update() or use Refresh() judiciously. (learn.microsoft.com)

What you observed matches how WinForms works: anything drawn via CreateGraphics() inside MouseClick can be overwritten on the next paint. Keep the rectangle’s position as a field, update it on click, and draw it only in Paint using e.Graphics. Microsoft’s guidance shows obtaining the Graphics from PaintEventArgs rather than creating your own. (learn.microsoft.com)

public partial class Form1 : Form
{
    private Rectangle _rect = new Rectangle(10, 10, 60, 40);
    private Rectangle _prev;

    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;   // reduce flicker
        this.ResizeRedraw = true;     // redraw on resize
        this.MouseClick += Form1_MouseClick;
        this.Paint += Form1_Paint;
    }

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        _prev = _rect;
        _rect.Location = new Point(e.X - _rect.Width / 2, e.Y - _rect.Height / 2);

        this.Invalidate(_prev);       // clear old spot
        this.Invalidate(_rect);       // draw new spot
        // Optional: this.Update();    // force immediate repaint if needed
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.DarkOrange, _rect);
        e.Graphics.DrawRectangle(Pens.Black, _rect);
    }
}

Notes:

  • ’s suggestion to call Invalidate() is the right trigger.
  • Avoid calling Form1_Paint(null, null) or base.OnPaint(null); the framework supplies PaintEventArgs when it repaints.

Recommended Answers

All 8 Replies

>how can i call paint event inside of mouseclick event?

You can handle all paint actions by getting/creating a reference of Graphics class.

Point oldp = new Point();
        Point newp = new Point();
        Graphics gc;
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            Graphics ps = this.CreateGraphics();
  
            oldp.X = newp.X;
            oldp.Y = newp.Y;
            newp = new Point(e.X, e.Y);
            
            ps.DrawLine(Pens.Red, newp, oldp);
        }

I've tried, but nothing happens when i click on the form...:( i don't know why, but drawing works only inside of Form1_Paint() event-method, WHY???

If you haven't added your own Paint event, you can use base.OnPain(null);

Calling this.Invalidate() will cause the Paint event to fire.

NO:( I've tried parentWindow_Panel2_Paint(null, null); and it seems to work but then after few seconds it gives error. Exception. I must give as a second argument PaintEventArgs, not null.....

Have you tried Control.Invalidate()? If all or part of a control is invalidated then a Paint event is raised.

Thanks, Ryshad!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.