I am just beginning to learn C# and I would like to seek advice on good practices when it comes to painting to avoid flickering.
I have used methods as mentioned on here, such as double buffering and turning on WS_EX_COMPOSITED
, but the method that works for me is:
Calling the PaintEventHandler
:
this.dgData.Paint += new System.Windows.Forms.PaintEventHandler(this.myPaint);
and using the PaintEventArgs
to draw:
private void myPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics myGraphics = e.Graphics;
// Use myGraphics to draw
}
And that the PaintEventHandler
is triggered via a Refresh()
on a Timer
.
private void TimerElapsed(object sender, System.EventArgs eventArgs)
{
Refresh();
}
However, my concern is that this way of drawing would require me to have a PaintEventHandler
for every control that I have on my form.
As such, I would like to ask if there is a more elegant way of accomplishing this.
Thanks!
I cannot tell any more where I found this code but this is what I do to remove flickering: I set these styles in my control's constructor: