How do I suspend control painting in C#


I would like to suspend control painting in C#, I have tried:
//Do operation here...

and it's not working, I have control (created at runtime) and I am using Controls["ControlName"] to access the properties.

LVL 12
Mohamed AbowardaSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can create property in your control. Say,
bool IsPaintingSuspended

Then override WndProc as below.
Setting/Resetting IsPaintingSuspended will disable/enable window painting.
bool IsPaintingSuspended { get; set; }

protected override void WndProc(ref Message m)
    const short WM_PAINT = 0x00F;

    if (m.Msg == WM_PAINT && IsPaintingSuspended)
        m.Result = IntPtr.Zero;

    base.WndProc(ref m);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You can use the approach here as well:
(replacing "this" with your control name)
Mohamed AbowardaSoftware EngineerAuthor Commented:
@lazyberezovsky, Idle_Mind
I have tried the code you have posted but still having a problem, I am trying to do changes to control at the same time for ex:

control.Left = X;
control.Top = Y;

I need to suspend the control and do any change on it, after that unsuspend the control, so that control.Left and control.Top will change (at the same time).

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

So, whats the problem with my code above?

control.IsPaintingSuspended = true;
control.Left = X;
control.Top = Y;
control.IsPaintingSuspended = false;

Consider doing control.Refresh() after enabling painting.
Mohamed AbowardaSoftware EngineerAuthor Commented:
control.Refresh() is flashing the control (the code used for multiple times)

The code works great, however there is still a problem with the parent form, I am using user control and after I change the control size and turn on redraw, the parent form was affected and appear as in the picture below.
Use Application.DoEnvent();
Mohamed AbowardaSoftware EngineerAuthor Commented:
I have tried Application.DoEnvent(); but the same problem still occur.
Mohamed AbowardaSoftware EngineerAuthor Commented:
It work, but there is a problem with the parent form.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.