2014-02-26 95 views
-3

我做了一個使用Writeline()更改.txt文件的程序。一切工作正常,只是我真的想知道是否有可能或不顯示寫作的進度,如果可能的話,如何。注意:我最近纔開始使用c#。如何使用WinForms中的進度條報告文件輸出進度?

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); 
      } 
     } 
+0

你是否使用過Google搜索,並找到了[如何使用WinForms進度條?](http://stackoverflow.com/questions/12126889/how-to-use-winforms-progress-bar) – user1477388

+0

請再考慮一下你的問題的標題。它應是問題/問題的*摘要*,而不是大多數隨機的單詞集合。 –

+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;只需要考慮一下...