Hias
Hias

Reputation: 55

windows form refresh - how can i make it smoother?

I draw a small labyrinth on a windows-form and re-draw it with every keydown-event. How can i make it smoother? The drawing durates approx. one second and when you enter the arrow buttons you can see a short flickering on the form.

        public void Draw_Labyrinth()
        {
            this.Update();
            this.SuspendLayout();

            Controls.Clear();


            for (int i = 0; i < list_Elements_Y.Count; i++)
            {

                for (int j = 0; j < list_Elements_Y[i].list_Elements_X.Count; j++)
                {

                    PictureBox pb = new PictureBox();

                    pb.Location = list_Elements_Y[i].list_Elements_X[j].position;
                    pb.Name = list_Elements_Y[i].list_Elements_X[j].name;
                    pb.Size = list_Elements_Y[i].list_Elements_X[j].size;


                    if (pb.Location == characterPosition) 
                    {
                        pb.Image = Properties.Resources.character;
                    }
                    else if (pb.Name == "item")
                    {
                        pb.Image = Properties.Resources.item;
                    }
                    else if (pb.Name == "wall")
                    {
                        pb.Image = Properties.Resources.wall;
                    }

                    Controls.Add(pb);


                    Application.DoEvents();
                    this.Update();
                }

            }

            Application.DoEvents();
            this.ResumeLayout();

        }

many thanks in advance!

Upvotes: 1

Views: 823

Answers (1)

rfmodulator
rfmodulator

Reputation: 3738

Flickering in WinForms can typically be solved (or greatly improved) by setting DoubleBuffered to true.

This can be done in the VS Designer, or in the Form constructor.

A full second of drawing operations seems excessive. So if you could optimize that it would be a good idea.

Upvotes: 2

Related Questions