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.

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.