ErocM
ErocM

Reputation: 4662

C# Label Text Not Updating

I have the following code:

private void button1_Click(object sender, EventArgs e)
{
  var answer =
    MessageBox.Show(
      "Do you wish to submit checked items to the ACH bank? \r\n\r\nOnly the items that are checked and have the status 'Entered' will be submitted.",
      "Submit",
      MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question,
      MessageBoxDefaultButton.Button1);

  if (answer != DialogResult.Yes)
    return;

  button1.Enabled = false;
  progressBar1.Maximum = dataGridView1.Rows.Count;
  progressBar1.Minimum = 0;
  progressBar1.Value = 0;
  progressBar1.Step = 1;

  foreach (DataGridViewRow row in dataGridView1.Rows)
  {
    if ((string) row.Cells["Status"].Value == "Entered")
    {
      progressBar1.PerformStep();

      label_Message.Text = @"Sending " + row.Cells["Name"].Value + @" for $" + row.Cells["CheckAmount"].Value + @" to the bank.";
      Thread.Sleep(2000);
    }
  }
  label_Message.Text = @"Complete.";
  button1.Enabled = true;
}

This is a test I am creating to port over to my application. Everything works fine but the label_Message.text being set. It never shows up on the screen. It is being set, I did a console.write on it to verify. It's just not refreshing the screen. I get the "Complete" at the end also.

Anyone have any ideas?

Upvotes: 14

Views: 45636

Answers (6)

Jollymorphic
Jollymorphic

Reputation: 3530

The Label doesn't re-paint until you give the UI thread back to the message loop. Try Label.Refresh, or better yet, try putting your lengthy operation in a background thread as other posters have suggested.

Upvotes: 5

user3884423
user3884423

Reputation: 594

I know this question is old but I had this same issue. I tried Refresh() and many other things but nothing worked. If I put the text into a Messagebox.show then it worked in the message box but not the form so I knew I had the data. As I had people waiting to use the app I was getting desperate and was just about to do away with the class temporarily to get it working when I thought of trying Invoke. So I tried

Invoke(new Action(() =>
        {
            lbltxt.Text = text;

        }));

For now it works but still not sure if this is a long term fix or just a plaster till I find a better solution.

Upvotes: 0

sommmen
sommmen

Reputation: 7608

Just to add to this answer, I ran into an issue with our splash screen form. We had code like this:


SplashScreen.Initialize(this, SplashScreenImage);
SplashScreen.Show();

// Db init, log init etc.

... Further in our app ...

Application.Run(new MainWindowForm());

The in Initialize(this, SplashScreenImage); we updated some controls and then we ran refresh on those controls;


public void Initialize(this, SplashScreenImage)
{
   ...

   lblVersion.Text = GetVersionString();
   lblEnv.Text = GetEnvironmentString();

   // Refresh (does not work)
   lblVersion.Refresh()
   lblEnv.Refresh()
}

Unfortunately, this does not work. The problem here was that although we call control.Refresh() explicitly, form.show() was called after we called control.refresh. This does not work.

The fix was simple:

SplashScreen.Show(); // First call show
SplashScreen.Initialize(this, SplashScreenImage); // Now contorl.Refresh() works

Upvotes: 0

Adam Sills
Adam Sills

Reputation: 17062

You're performing a lengthy operation on the UI thread. You should move it to a background thread (via BackgroundWorker for instance) so the UI thread can do things like repaint the screen when needed. You can cheat and execute Application.DoEvents, but I'd really recommend against it.

This question and answer are basically what you're asking:
Form Not Responding when any other operation performed in C#

Upvotes: 25

enriquein
enriquein

Reputation: 1038

This usually happens when you're doing intensive calculations/iterations in the same thread as where the user interface elements are running. To work around this you're going to need to have a separate thread do the work and from there update the label's value accordingly. I'd post a complete source sample but I'm away from my dev machine at the moment.

Upvotes: 0

MadBender
MadBender

Reputation: 1458

This operation is executed in UI thread. UI won't update until it's finished. To make it update during sending you must perform sending in separate thread and update the label from there

Upvotes: 1

Related Questions