Avoid controls flickering while resizing them by code (.NET 4.6.1)

62 Views Asked by At

I tried searching the site but didn't find any solution to my problem.
I would like to dynamically change my controls size inside the form and see only the final result without flickering issues.
I have written a short example to show you my problem:

When I press the "Bigger" button I would like to change my form from this:
1

to this:
2

without seeing this:3

    public Form1()
    {
        InitializeComponent();

        //DoubleBuffered = true; // Useless
    }

    private void Smaller_Click(object sender, EventArgs e)
    {
        while (listBox1.Width > 100)
        {
            listBox1.Width -= 10;
            pictureBox1.Width -= 10;
            progressBar1.Width -= 10;
            textBox1.Width -= 10;
            Thread.Sleep(25);
        }
    }

    private void Bigger_Click(object sender, EventArgs e)
    {
        //this.SuspendLayout(); // Useless
        while(listBox1.Width < 700)
        {
            listBox1.Width += 10;
            pictureBox1.Width += 10;
            progressBar1.Width += 10;
            textBox1.Width += 10;
            Thread.Sleep(25);
        }
        //this.ResumeLayout();
    }

This is only an example, my code is way different so I can not make all the resizing in one step.

I already tried DoubleBuffer = true and SuspendLayout() without any success.

Thanks

0

There are 0 best solutions below