-2

I want to display progress of saving a file using the "SaveFileDialog" and "Progress Bar" in a windows form. The file being saved is text file (rtf, txt, ...). This is what I use to save a file:

  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.";
        }
    }

The GetCurrentDocument is this:

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

Thus, SaveFile() is:

RichTextBox.SaveFile Method: Saves the contents of the RichTextBox to a file. msdn page

Somehow I want to display the saving on the progress bar, or at least display a notice when the saving is finished (when the file is in the final location).

Morgosus
  • 807
  • 5
  • 18
  • 1
    That completely depends on what `SaveFile()` does. – SLaks Jul 24 '17 at 18:05
  • It's RichTextBox.SaveFile Method. – Morgosus Jul 24 '17 at 18:34
  • 1
    Unless your RichTextBox is gigantically long with text, saving should be a relatively short process. And event if it does take a long time, you don't have access to an enumerator that tells you the saving progress. Unknown long operations would use a marquee progress in those cases. Just put `StatusBar.Text = "Save Completed."; after your SaveFile line. – LarsTech Jul 24 '17 at 18:42

2 Answers2

1

You will need a progress bar as a Marquee style as you don't know how long it will take to save the file. Try this

Rob Anthony
  • 1,743
  • 1
  • 13
  • 17
0

what kinf of class is getcurrentdocument and what savefile method do. Could You use BackgroundWorker for save the file and open an progress bar in the ui waiting for a signal of the end of the workprocess.

Howto

Write me if this solution it isn't enought.