C#标签文字未更新 [英] C# Label Text Not Updating

查看:102
本文介绍了C#标签文字未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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;
}

这是我要移植到我的应用程序上的测试.一切正常,但设置了label_Message.text.它永远不会显示在屏幕上.设置好了,我在上面做了console.write来验证.只是不刷新屏幕.我也获得了完成"字样.

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.

有人有什么主意吗?

推荐答案

您正在UI线程上执行冗长的操作.您应该将其移至后台线程(例如,通过BackgroundWorker),以便UI线程可以在需要时执行诸如重新绘制屏幕的操作.您可以作弊并执行Application.DoEvents,但我真的建议您反对.

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.

这个问题和答案基本上就是您要问的:
在C#中执行任何其他操作时表单无响应

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

这篇关于C#标签文字未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆