Updating toolStripStatusLabel immediately

10.7k Views Asked by At

I have a WinForms program that has a button, a statusStrip, and a toolStripStatusLabel in that statusStrip. If I click the button and this runs:

private void button1_Click(object sender, EventArgs e)
{
    toolStripStatusLabel1.Text = "Test";

    System.Threading.Thread.Sleep(5000);
}

Then the toolStripStatusLabel's text doesn't update until after the thread is done sleeping. How do I get it to update immediately, then sleep the thread?

5

There are 5 best solutions below

0
derekantrican On BEST ANSWER

As P. Kouvarakis said, the solution was this:

toolStripStatusLabel1.Text = "Test";
statusStrip1.Update();
0
Yiannis Leoussis On
toolStripStatusLabel1.Text = "Test";
Application.DoEvents();
2
HRolle On

Last solution with Application.DoEvents() is the best. Application.DoEvents() must be appended to each StatusStrip change:

private void SetParseStatus(object sender, ParseStatusChangedEventArgs e)
{
    toolStripProgressBar1.Value = e.Percent;
    Application.DoEvents();
}
0
C.M. On

My status label change wasn't showing at runtime when the status strip had Spring set to true. I set Spring to false and it began showing.

0
Vano Sz On
  1. You need to add the StatusStrip text label (as a child) ('cause you can't manipulate with the text of a parent component) like this:

child component

  1. When you double-click on statusstrip component you will see the next

2ble click pic

Do NOT add the text change code here, cause it will be refreshed only after a click on it (as you can see on the picture - statusStrip1_ItemClicked) so, any statusStrip1.Update(); this.Update(); etc can't help you.

  1. How to do it quickly. You can add the if where the target condition started like in this picture

example "if"