How do I call paint event?

C#.NetWinformsEvents

C# Problem Overview


My program draws text on its panel,but if I'd like to remove the text I have to repaint.

How do I call(raise) the paint event by hand?

C# Solutions


Solution 1 - C#

In a method of your Form or Control, you have 3 choices:

this.Invalidate();  // request a delayed Repaint by the normal MessageLoop system    
this.Update();      // forces Repaint of invalidated area 
this.Refresh();     // Combines Invalidate() and Update()

Normally, you would just call Invalidate() and let the system combine that with other Screen updates. If you're in a hurry you should call Refresh() but then you run the risk that it will be repainted several times consecutively because of other controls (especially the Parent) Invalidating.

The normal way Windows (Win32 and WinForms.Net) handles this is to wait for the MessageQueue to run empty and then process all invalidated screen areas. That is efficient because when something changes that usually cascades into other things (controls) changing as well.

The most common scenario for Update() is when you change a property (say, label1.Text, which will invalidate the Label) in a for-loop and that loop is temporarily blocking the Message-Loop. Whenever you use it, you should ask yourself if you shouldn't be using a Thread instead. But the answer is't always Yes.

Solution 2 - C#

The Invalidate() Method will cause a repaint.

MSDN Link

Solution 3 - C#

I found the Invalidate() creating too much of flickering. Here's my situation. A custom control I am developing draws its whole contents via handling the Paint event.

this.Paint += this.OnPaint;

This handler calls a custom routine that does the actual painting.

private void OnPaint(object sender, PaintEventArgs e)
{
    this.DrawFrame(e.Graphics);
}

To simulate scrolling I want to repaint my control every time the cursor moves while the left mouse button is pressed. My first choice was using the Invalidate() like the following.

private void RedrawFrame()
{
    var r = new Rectangle(
        0, 0, this.Width, this.Height);

    this.Invalidate(r);
    this.Update();
}

The control scrolls OK but flickers far beyond any comfortable level. So I decided, instead of repainting the control, to call my custom DrawFrame() method directly after handling the MouseMove event. That produced a smooth scrolling with no flickering.

private void RedrawFrame()
{
    var g = Graphics.FromHwnd(this.Handle);
    this.DrawFrame(g);
}

This approach may not be applicable to all situations, but so far it suits me well.

Solution 4 - C#

I think you can also call Refresh().

Solution 5 - C#

Maybe this is an old question and that´s the reason why these answers didn't work for me ... using Visual Studio 2019, after some investigating, this is the solution I've found:

this.InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.DisplayRectangle));

Solution 6 - C#

Call control.invalidate and the paint event will be raised.

Solution 7 - C#

Refresh would probably also make for much more readable code, depending on context.

Solution 8 - C#

use Control.InvokePaint you can also use it for manual double buffering

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIvan ProdanovView Question on Stackoverflow
Solution 1 - C#Henk HoltermanView Answer on Stackoverflow
Solution 2 - C#Matthew VinesView Answer on Stackoverflow
Solution 3 - C#user2529145View Answer on Stackoverflow
Solution 4 - C#BrianView Answer on Stackoverflow
Solution 5 - C#Izar UrdinView Answer on Stackoverflow
Solution 6 - C#Otávio DécioView Answer on Stackoverflow
Solution 7 - C#Simon NunnView Answer on Stackoverflow
Solution 8 - C#TheNegativeView Answer on Stackoverflow