Note that there are some explanatory texts on larger screens.

plurals
  1. POForeach loop to create 100 buttons, painting all buttons at same time as to prevent flicker
    primarykey
    data
    text
    <p>In my minesweeper game I need to dynamically create controls in order to shift between easy - medium - hard. Let's say for the sake of the question hard consists of 100 buttons.</p> <p>This is how I'm creating them:</p> <pre><code>this.SuspendLayout(); //Creating so many things that I assume it would be faster to suspend and resume. foreach (string i in playingField) { Button button = new Button(); button.Name = i; button.FlatStyle = System.Windows.Forms.FlatStyle.Popup; button.Margin = new Padding(0); button.TabIndex = 0; button.Location = new System.Drawing.Point(3, 3); button.Size = new System.Drawing.Size(25, 25); button.BackgroundImage = blockImage; //##// button.MouseDown += new System.Windows.Forms.MouseEventHandler(this.GetUnderSide); button.UseVisualStyleBackColor = false; minesPanel.Controls.Add(button); //Adding the controls to the container } this.ResumeLayout(); //Refer to above. Correct me if I'm wrong please. </code></pre> <p>As you can see I'm creating all of the controls through a for loop, and then adding them. It results in each button being painted once at a time. I also tried <code>name.Controls.AddRange(arrayButtons)</code> and it still resulted in the same problem. Individual painting.</p> <p>What I need is a way to create all items, and then paint them ALL after, like painting a single bitmap with <code>DoubleBuffered</code>, but alas, that doesn't work either.</p> <p>Furthermore, I have tried this:</p> <pre><code> protected override CreateParams CreateParams { get { // Activate double buffering at the form level. All child controls will be double buffered as well. CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; // WS_EX_COMPOSITED return cp; } } </code></pre> <p>Which kind of works. It only works on application startup. However, given that I will be changing grid sizes and adding more controls at runtime, it is not a viable option. </p> <p>I've been told it's as easy as using the methods in the <code>Graphics</code> class. Problem is, I don't know where to start. </p> <p>If anyone could provide some help on getting all my controls to be painted at the same time, that would be great.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload