简体   繁体   English

C# 标签文本不更新,而进度条值是

[英]C# Label text not updating while Progress bar value is

In C# im trying to make a little game type program and im trying to make a loading bar that uses the Progress bar and the text is using a Label, for example the Progress bar is 1 - 25 and i want the label text to update while the bar is, heres an example:在 C# 中,我试图制作一个小游戏类型的程序,我试图制作一个使用进度条的加载栏,文本使用标签,例如进度条是 1 - 25,我希望标签文本在更新时更新酒吧是,这是一个例子:

    private void StartLoading_Click(object sender, EventArgs e)
    {
        MainProgressBar.Maximum = 25;
        int P = 0;
        while (P < 25)
        {
            // Delay
            System.Threading.Thread.Sleep(130);
            // Increase Progress
            P++;
            // Set Progress Bar Value
            MainProgressBar.Value = P;
            // Set Text Above Progress Bar
            LoadingText.Text = P + "/25";
        }
    }

Ps.附言。 I dont want some Huge code, i want it to be simple like this我不想要一些庞大的代码,我希望它像这样简单

State of the art is this snippet for you:最先进的是这个片段给你:

private void StartLoading_Click(object sender, EventArgs e)
    {
        const int max = 25;
        var progressHandler = new Progress<int>(value=>{
            LoadingText.Text = value + "/" + max;
            MainProgressBar.Value = value;
        });

        var progress = progressHandler as IProgress<int>;
        await Task.Run(() =>
        {
           int P = 0;
           while (P < 25)
           {
              Thread.Sleep(130);
              progress?.Report(++P);
           }
        }       
    }

This processes your long running task (Sleep in this case), in a seperate Thread and reuse the value via the Progress-Class.这会在单独的线程中处理您长时间运行的任务(在这种情况下为睡眠),并通过 Progress-Class 重用该值。 This way your GUI is updated in GUI-Thread as recommended and you will get the updates accordingly.这样您的 GUI 就会按照建议在 GUI-Thread 中更新,您将获得相应的更新。 Further it's not recommended to use Application.DoEvents();此外,不建议使用Application.DoEvents(); , because there are many pitfalls you have to know about. ,因为您必须了解许多陷阱

//This is what you are exactly looking for.. //这就是你要找的..

namespace WindowsFormsApplication1 { public partial class Form1: Form { public Form1() { InitializeComponent(); namespace WindowsFormsApplication1 { public partial class Form1: Form { public Form1() { InitializeComponent(); } }

    private void timer1_Tick(object sender, EventArgs e)
    {
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;
        progressBar1.Value = progressBar1.Value + 1;

        //this.progressBar1.Increment(1);

        if (progressBar1.Value <= 0)
        {
            lblProgress.Text = "Initializing components.......";
        }
        else if (progressBar1.Value <= 10)
        {
            lblProgress.Text = "Loading fonts & Modules........";
        }
        else if (progressBar1.Value <= 20)
        {
            lblProgress.Text = "Appliying themes and graphics.......";
        }
        else if (progressBar1.Value <= 30)
        {
            lblProgress.Text = "Collecting Information........";
        }
        else if (progressBar1.Value <= 40)
        {
            lblProgress.Text = "Updating registry........";
        }
        else if (progressBar1.Value <= 50)
        {
            lblProgress.Text = "Fetching Information........";
        }
        else if (progressBar1.Value <= 80)
        {
            lblProgress.Text = "Finalizing........";
        }
        else if (progressBar1.Value <= 90)
        {
            lblProgress.Text = "Almost finished.......";
        }
        else if (progressBar1.Value >= progressBar1.Maximum)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
            this.Hide();
            timer1.Stop();
            //progressBar1.Dispose();
        }

        else
        {


        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM