Aryan SuryaWansi
Aryan SuryaWansi

Reputation: 2741

What does invalidate method do?

What does invalidate method do in winform app?

Invalidate() method comes with six overloaded form inside control class of System.Windows.Forms namespace.

Thanks.....

Upvotes: 17

Views: 33146

Answers (6)

Martin Liversage
Martin Liversage

Reputation: 106836

Windows Forms uses GDI for rendering. GDI is the original graphics interface in Windows. DirectX is a newer interface originally created for games development but now also used by higher level frameworks like WPF.

GDI is based around the concept of a paint method. When a window is displayed Windows will send a paint message to the code responsible for the window. This will lead to the paint method being called. The paint method should then paint the contents of the window onto the screen.

When a GDI program wants to update what is displayed it cannot directly paint the updated image onto the screen. Instead it has to tell Windows that an area needs to be updated. This is called invalidating a region. Windows will then call the relevant paint method supplying information about what is invalid and needs updating. The paint method should then draw the updated contents to the screen.

This method of updating screen contents is also used when windows are dragged across other windows. When GDI was developed graphics hardware was pretty slow and a lot of work is done inside Windows to cache bitmaps and to only invalidate and update what is changed.

When overlapping windows or child windows are drawn it is done back to front to get the correct layering of visual elements. This can lead to flicker where a background is erased and drawn followed by other elements in front. If the redraw speed is slower than the screen refresh you might notice some flickering. This is a telltale sign of a GDI application perhaps created using Windows Forms.

In Windows Forms when you invalidate a control you request that it should be redrawn.

Upvotes: 44

Rauf
Rauf

Reputation: 12842

The Invalidate() method will redraw the control. For example if you use a panel 'panel1', which contains a label and a text box, the following code will redraw both the label and text box (by calling the Paint event)

panel1.Invalidate();

Upvotes: 2

asawyer
asawyer

Reputation: 17808

Asks windows to redraw the client area of the invalidated window.

From MSDN:

"Invalidates the entire surface of the control and causes the control to be redrawn."

http://msdn.microsoft.com/en-us/library/598t492a.aspx

Upvotes: 6

Mark Arnott
Mark Arnott

Reputation: 1994

It causes the control to be repainted. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate.aspx

You will rarely need to call this method unless you are doing some low level graphics manipulation.

Upvotes: 3

LarsTech
LarsTech

Reputation: 81620

It basically calls the PaintBackground and Paint methods of the control.

Upvotes: 1

The Evil Greebo
The Evil Greebo

Reputation: 7138

It's a GUI rendering method - it forces windows to redraw the visible portion of the control.

Upvotes: 1

Related Questions