我想在Windows窗体中使用“SaveFileDialog”和“Progress Bar”显示保存文件的进度。保存的文件是文本文件(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方法:将RichTextBox的内容保存到文件中。 msdn page
不知何故,我想在进度条上显示保存,或者至少在保存完成时显示通知(当文件位于最终位置时)。