如何使用 savefiledialog 在进度条上显示进度?

How to display progress on a progress bar using savefiledialog?

我想使用 "SaveFileDialog" 和 "Progress Bar" 以 windows 形式显示保存文件的进度。正在保存的文件是文本文件(rtf、txt、...)。这是我用来保存文件的方式:

  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 Method: Saves the contents of the RichTextBox to a file. msdn page

我想以某种方式在进度条上显示保存,或者至少在保存完成时(当文件位于最终位置时)显示一个通知。

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

Howto

如果此解决方案不够用,请写信给我。

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