如何在FOR中每次显示标签(对于每个循环) [英] How to Show the Label eachtime in FOR (for every loop)

查看:133
本文介绍了如何在FOR中每次显示标签(对于每个循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在winform项目中工作。我在FOR LOOP中有标签。我希望每次执行label.text语句后都显示标签。但它并不是每次都显示而是在for循环结束后显示。



我试图通过使用Thread sleep实现这一点。但我不能。请帮帮我。谢谢。



这是我的编码。



hi all, am working in winform project . where i have label in FOR LOOP .I want to show the label each time after executing label.text statement . But it doesn't show for every time rather it shows after for loop is finished .

I tried to achieve this by using Thread sleep . But i can't. Please help me .Thank You

Here's My Coding .

for (int i = 1; i <= sourceTable.Rows.Count - 1; i++)
             {

            
                 string checkout;
                  checkout= sourceTable.Rows[i].Field<string>(0);

                  dest = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString);
                  dest.Open();
               destcmd = new SqlCommand(checkout, dest);
               destcmd.ExecuteNonQuery();
               dest.Close();


               prcmail();
               prcmessagecheck();




            
      lblProgress.Text = "Hello "+i;

           

             Thread.Sleep(10000);







                }

推荐答案

假设你的'for循环没有在一个单独的线程中运行:



1.正如Maciej推荐的那样,你需要处理打开外面的连接循环



考虑:



Assuming your 'for loop is not running in a separate thread:

1. as Maciej recommended, you need to handle opening the connection outside the loop

Consider:

// open the connection

// set an initial message in the TextBox
lblProgress.Text = "Connected. Completing: ";

// in the loop: update
lblProgress.Text += i.ToString() + ", ";

// after the loop completes: clean-up
lblProgress.Text = lblProgress.Text.Trim().TrimEnd(',');

这样就没有必要使用'Sleep来减慢你的处理速度。



但是,为什么不考虑使用MS ProgressBar Control?

This way there's no need to slow your processing down with using 'Sleep.

However, why not consider using the MS ProgressBar Control ?


Windows窗体程序在一个线程中运行所有内容 - UI线程。使用Invalidate / Update代码告诉编译器暂停函数的执行并强制它更新UI而不是等到函数结束。因此,在设置label.text值后,使用Invalidate和update只需使用这两种方法。快乐的编码。所以代码中的所有内容都是正确的,除非缺少这两行。只需添加并查看所需结果即可。快乐的编码

Windows Forms Programs run everything in a single thread - the UI thread. Using the Invalidate/Update code tells the compiler to "pause" execution of the function and forces it to update the UI instead of waiting till the end of the function. So use Invalidate and update just use these two methods after setting label.text value. happy coding. So everything in your code is correct except these two line is missing. Just add and see your desired result. happy coding
for (int i = 1; i <= sourceTable.Rows.Count - 1; i++)
{
    string checkout;
    checkout = sourceTable.Rows[i].Field<string>(0);

    dest = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString);
    dest.Open();
    destcmd = new SqlCommand(checkout, dest);
    destcmd.ExecuteNonQuery();
    dest.Close();
    prcmail();
    prcmessagecheck();
    lblProgress.Text = "Hello " + i;

    lblProgress.Invalidate();  //add
    lblProgress.Update();       // add

    Thread.Sleep(10000);
}


您是否尝试在更新文本后使用refresh()方法......就像这样...



Have you tried to use refresh() method after updating text... like this...

lblProgress.Text = "Hello" +i;
Refresh();


这篇关于如何在FOR中每次显示标签(对于每个循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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