ControlHelper.SuspendDrawing(panel);
panel.Controls.Clear();
AddItemIdLabel();
AddLastEditedLabel();
AddDeleteButton();
AddSaveButton();
ControlHelper.ResumeDrawing(panel);
public static class ControlHelper
{
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private const int WM_SETREDRAW = 0xB;
public static void SuspendDrawing(Control target)
{
SendMessage(target.Handle, WM_SETREDRAW, 0, 0);
}
public static void ResumeDrawing(Control target)
{
SendMessage(target.Handle, WM_SETREDRAW, 1, 0);
target.Refresh();
}
}
If I test with the code above, parts of the panel aren't being refreshed. You can see the old controls from before the Clear() on places where no new controls have been added.
If I put the panel.Controls.Clear();
before the ControlHelper.SuspendDrawing(panel);
everything works as intented but some flickering is visible which I'm trying to avoid.
So what's going on here? How can depending on whether I clear the collection of controls before or after the suspend make a difference?
Control.Refresh()
is exactly a combination ofInvalidate(true) + Update()
. If it's overridden by a child control, it's probably for a good reason. – Wersh