如何使用WinForms中的进度条报告文件输出的进度?

时间:2014-02-26 20:35:44

标签: c# winforms

我创建了一个使用Writeline()更改.txt文件的程序。一切都很好,只是我真的想知道是否可以显示写作的进展,如果可能的话,如何可能。注意:我最近才开始使用c#。

    private void button1_Click(object sender, EventArgs e)
    {
        string res = "";
        {
            if (checkBox1.Checked == true) { res = "playercontrols:1:up-w,right-d,left-a,aimy-,run-lshift,reload-r,portal2-,portal1-,jump- ,aimx-,down-s,use-e;playercontrols:2:up-joy-1-hat-1-u,right-joy-1-hat-1-r,left-joy-1-hat-1-l,aimy-joy-1-axe-4-neg,run-joy-1-but-3,reload-joy-1-but-4,portal2-joy-1-but-6,portal1-joy-1-but-5,jump-joy-1-but-1,aimx-joy-1-axe-5-neg,down-joy-1-hat-1-d,use-joy-1-but-2;playercontrols:3:up-joy-2-hat-1-u,right-joy-2-hat-1-r,left-joy-2-hat-1-l,aimy-joy-2-axe-4-neg,run-joy-2-but-3,reload-joy-2-but-4,portal2-joy-2-but-6,portal1-joy-2-but-5,jump-joy-2-but-1,aimx-joy-2-axe-5-neg,down-joy-2-hat-1-d,use-joy-2-but-2;playercontrols:4:up-joy-3-hat-1-u,right-joy-3-hat-1-r,left-joy-3-hat-1-l,aimy-joy-3-axe-4-neg,run-joy-3-but-3,reload-joy-3-but-4,portal2-joy-3-but-6,portal1-joy-3-but-5,jump-joy-3-but-1,aimx-joy-3-axe-5-neg,down-joy-3-hat-1-d,use-joy-3-but-2;playercolors:1:224,32,0,136,112,0,252,152,56;playercolors:2:255,255,255,0,160,0,252,152,56;playercolors:3:0,0,0,200,76,12,252,188,176;playercolors:4:32,56,236,0,128,136,252,152,56;portalhues:1:0,0.125;portalhues:2:0.25,0.375;portalhues:3:0.5,0.625;portalhues:4:0.75,0.875;mariohats:1:1;mariohats:2:1;mariohats:3:1;mariohats:4:1;scale:3;shader1:none;shader2:none;volume:1;mouseowner:1;mappack:smb;gamefinished;"; }
            else { res = "playercontrols:1:up-w,right-d,left-a,aimy-,run-lshift,reload-r,portal2-,portal1-,jump- ,aimx-,down-s,use-e;playercontrols:2:up-joy-1-hat-1-u,right-joy-1-hat-1-r,left-joy-1-hat-1-l,aimy-joy-1-axe-4-neg,run-joy-1-but-3,reload-joy-1-but-4,portal2-joy-1-but-6,portal1-joy-1-but-5,jump-joy-1-but-1,aimx-joy-1-axe-5-neg,down-joy-1-hat-1-d,use-joy-1-but-2;playercontrols:3:up-joy-2-hat-1-u,right-joy-2-hat-1-r,left-joy-2-hat-1-l,aimy-joy-2-axe-4-neg,run-joy-2-but-3,reload-joy-2-but-4,portal2-joy-2-but-6,portal1-joy-2-but-5,jump-joy-2-but-1,aimx-joy-2-axe-5-neg,down-joy-2-hat-1-d,use-joy-2-but-2;playercontrols:4:up-joy-3-hat-1-u,right-joy-3-hat-1-r,left-joy-3-hat-1-l,aimy-joy-3-axe-4-neg,run-joy-3-but-3,reload-joy-3-but-4,portal2-joy-3-but-6,portal1-joy-3-but-5,jump-joy-3-but-1,aimx-joy-3-axe-5-neg,down-joy-3-hat-1-d,use-joy-3-but-2;playercolors:1:224,32,0,136,112,0,252,152,56;playercolors:2:255,255,255,0,160,0,252,152,56;playercolors:3:0,0,0,200,76,12,252,188,176;playercolors:4:32,56,236,0,128,136,252,152,56;portalhues:1:0,0.125;portalhues:2:0.25,0.375;portalhues:3:0.5,0.625;portalhues:4:0.75,0.875;mariohats:1:1;mariohats:2:1;mariohats:3:1;mariohats:4:1;scale:3;shader1:none;shader2:none;volume:1;mouseowner:1;mappack:smb;"; }
            if (checkBox2.Checked == true) { res = res + "reachedworlds:smb:1,"; }
            else { res = res + "reachedworlds:smb:0,"; }
            if (checkBox3.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox4.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox5.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox6.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox7.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox8.Checked == true) { res = res + "1,"; }
            else { res = res + "0,"; }
            if (checkBox9.Checked == true) { res = res + "1;"; }
            else { res = res + "0;"; }
        }
        DialogResult ans=MessageBox.Show("Warning! All settings in your game will be reset. Do you wish to continue?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
        if (ans == DialogResult.OK)
        {
            string roaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string path = roaming + "\\LOVE\\mari0\\options.txt";
            using (StreamWriter sr = new StreamWriter(path))
            {
                sr.Write(res);
            }
        }

1 个答案:

答案 0 :(得分:0)

假设您使用的是基于其中一个标记的winforms,您可以执行以下操作:

public Form1()
{
    InitializeComponent();
    backgroundWorker1.WorkerReportsProgress = true;
}

private void WriteToFileMethod()
{
    // your routine here
}

private void button_Click(object sender, EventArgs e)
{
    progressBar.Maximum = 100;
    progressBar.Step = 1;
    progressBar.Value = 0;
    backgroundWorker.RunWorkerAsync();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    var backgroundWorker = sender as BackgroundWorker;
    for (int i = 0; i < workSize; i++)
    {
        WriteToFileMethod();
        backgroundWorker.ReportProgress((i * 100) / workSize);
    }
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

只需将 workSize 替换为您尝试执行的工作的大小。您还可以调整该代码以报告您正在编写的行数或文件数的进度。这取决于你究竟要报道的内容。你甚至可以做内存大小。

MSDN在解释所有细节here方面做得非常出色。除了看我的例子,我强烈建议阅读他们的概述。它将提供对需要完成的内容的更详细的了解。

最后,进度条可能并不总是可行的选择。您可以实现一些让用户知道后台进程正在发生并且进程没有崩溃的东西是WaitCursor:Cursor.Current = Cursor.WaitCursor;只需要考虑一下......