Note that there are some explanatory texts on larger screens.

plurals
  1. POLabel text doesn't get updated until the whole loop is completed
    primarykey
    data
    text
    <p>I have a Winform program that does some calculations when the user clicks on a button and then calls the picturebox paint event to draw a new BMP based on the results of the calculations. This works fine.</p> <p>Now I want to do this 100 times and every time the picturebox is refreshed, I want to see the iteration that it's currently in by updating the text on a label as per below:</p> <pre><code> private void button2_Click(object sender, EventArgs e) { for (int iterations = 1; iterations &lt;= 100; iterations++) { // do some calculations to change the cellmap parameters cellMap.Calculate(); // Refresh picturebox1 pictureBox1.Invalidate(); pictureBox1.Update(); // Update label with the current iteration number label1.Text = iterations.ToString(); } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Bitmap bmp = new Bitmap(cellMap.Dimensions.Width, cellMap.Dimensions.Height); Graphics gBmp = Graphics.FromImage(bmp); int rectWidth = scaleFactor; int rectHeight = scaleFactor; // Create solid brushes Brush blueBrush = new SolidBrush(Color.Blue); Brush greenBrush = new SolidBrush(Color.Green); Brush transparentBrush = new SolidBrush(Color.Transparent); Graphics g = e.Graphics; for (int i = 0; i &lt; cellMap.Dimensions.Width; i++) { for (int j = 0; j &lt; cellMap.Dimensions.Height; j++) { // retrieve the rectangle and draw it Brush whichBrush; if (cellMap.GetCell(i, j).CurrentState == CellState.State1) { whichBrush = blueBrush; } else if (cellMap.GetCell(i, j).CurrentState == CellState.State2) { whichBrush = greenBrush; } else { whichBrush = transparentBrush; } // draw rectangle to bmp gBmp.FillRectangle(whichBrush, i, j, 1f, 1f); } } g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; g.DrawImage(bmp, 0, 0, pictureBox1.Width, pictureBox1.Height); } </code></pre> <p>The problem I am having is that the label text only gets displayed after the last picturebox update is completed. So essentially, it does not display 1 through to 99. I can see the picturebox updates after every refresh as the BMP changes with every iteration. Any idea?</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