Begin and End Drawing function in C#
-
Hi, Actually I want to draw many rectangles on the screen and want to show all of them at once. Is there any function like BeginUpdate and EndUpdate in c#. so that the drawing process is not shown to the viewer.
-
Hi, Actually I want to draw many rectangles on the screen and want to show all of them at once. Is there any function like BeginUpdate and EndUpdate in c#. so that the drawing process is not shown to the viewer.
No there isn't really, what you want is double-buffering, if you're using .NET 2 then there should be a
DoubleBuffered
property on the control you're inheriting from. If not then usebase.SetStyle
in the constructor of the class. TheBeginUpdate
andEndUpdate
are used in a different way, say you have aListBox
, if you want to add 1000 items to it normally each time you add an item theInvalidate
method is called, this will cause the control to recalculate it's entire contents and redraw all of them. If you use theBegin/EndUpdate
pair then what this does is set a flag to say that when an item is added / deleted changed it's not meant to be redrawn. When you call theEndUpdate
function when all the items have been added then it calls theInvalidate
method to redraw the control (and clears the flag). You can see the obvious benefits, in the first method (noBegin/EndUpdate
) the control is redrawn 1000 times as each item is added (thus producing a flicker (unless you have an awesomely specced machine)), in the second it only redraws once when everything has finished. -
No there isn't really, what you want is double-buffering, if you're using .NET 2 then there should be a
DoubleBuffered
property on the control you're inheriting from. If not then usebase.SetStyle
in the constructor of the class. TheBeginUpdate
andEndUpdate
are used in a different way, say you have aListBox
, if you want to add 1000 items to it normally each time you add an item theInvalidate
method is called, this will cause the control to recalculate it's entire contents and redraw all of them. If you use theBegin/EndUpdate
pair then what this does is set a flag to say that when an item is added / deleted changed it's not meant to be redrawn. When you call theEndUpdate
function when all the items have been added then it calls theInvalidate
method to redraw the control (and clears the flag). You can see the obvious benefits, in the first method (noBegin/EndUpdate
) the control is redrawn 1000 times as each item is added (thus producing a flicker (unless you have an awesomely specced machine)), in the second it only redraws once when everything has finished.Hi, thanks for reply, actually I am drawing almost 10000 rectangles on the screen. My main function of drawing is called in the form PAINT function. I have enabled the double buffered property but it is not working. Can you give me a bit more detail? Thanks for help.
-
Hi, thanks for reply, actually I am drawing almost 10000 rectangles on the screen. My main function of drawing is called in the form PAINT function. I have enabled the double buffered property but it is not working. Can you give me a bit more detail? Thanks for help.
My only suggestion would be to move any position calculations to outside the
Paint
function because that is called more often. I.e. do as few mathematical calculations for the positioning / size of the elements in thePaint
event, do that inside theResize
.