2017-07-24 61 views
-2

我需要使用窗体中的“SaveFileDialog”和“Progress Bar”来显示保存文件的进度。正在保存的文件是文本文件(rtf,txt,...)。这是我用它来保存文件:通过进度条(Windows窗体)上的savefiledialog显示保存进度?

private void Save() 
    { 
     if (TabControl.TabPages.Count != 0) 
     { 
      SaveFileDialog.FileName = TabControl.SelectedTab.Name; 
      SaveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //default path 
      SaveFileDialog.Filter = "Rich Text Format|*.rtf";//Extensions 
      SaveFileDialog.Title = "Save"; 

      if (SaveFileDialog.ShowDialog() == DialogResult.OK) 
      { 
       if (SaveFileDialog.FileName.Length > 0) 
       { 
        GetCurrentDocument.SaveFile(SaveFileDialog.FileName, RichTextBoxStreamType.RichText);//Stream Type for .rtf 
       } 
      } 
      else if (SaveFileDialog.ShowDialog() == DialogResult.Cancel) 
      { 
       StatusBar.Text = "Saving Cancelled."; //Makes sure it doesn't crash on cancel 
      } 
     } 
     else 
     { 
      StatusBar.Text = "Can't save. No tabs are detected."; 
     } 
    } 

的GetCurrentDocument是这样的:

private RichTextBox GetCurrentDocument 
    { 
     get 
     { 
      return 
       (RichTextBox)TabControl.SelectedTab.Controls["Body"]; 
     } 
    } 

因此,SAVEFILE()是:

RichTextBox.SaveFile方法:保存内容RichTextBox的一个文件。 msdn page

不知何故我需要的进度条来显示的保存,或至少显示时的保存结束(当该文件是在最后的位置)。

+1

这完全取决于'SaveFile()'的作用。 – SLaks

+0

它是RichTextBox.SaveFile方法。 –

+1

除非您的RichTextBox文字太长,否则保存应该是一个相对较短的过程。而事件如果确实需要很长时间,则无法访问能够告诉您存储进度的枚举器。在这些情况下,未知的长操作会使用选框进度。只要把'StatusBar.Text =“保存完成。”;在您的SaveFile行之后。 – LarsTech

回答

0

您将需要一个进度条作为选取框样式,因为您不知道保存该文件需要多长时间。 Try this

0

类的什么类是getcurrentdocument和savefile方法。 您可以使用BackgroundWorker保存文件并在ui中打开一个进度条,等待工作进程结束的信号。

Howto

收件箱,如果这个解决方案是不是足够多。