Question

I have a SaveFileDialog in my program. The issue is that when I click "Cancel" on the dialog, another SaveFileDialog opens up. But when I click cancel on the second SaveFileDialog, a third does NOT appear, so it's not a loop or anything like that. I can't see what is causing my SaveFileDialog to behave in such an odd manner. Obviously I need to fix this so that if the user clicks cancel on the first SaveFileDialog, it returns them to the form.

The code for saving in my program is as follows:

 private void SaveFile()
    {
        if (filepath == null)
        {
            SaveFileAs();
            }

        else
        {
            StreamWriter sw = new StreamWriter(filepath);
            try
            {
                sw.WriteLine(richTextBoxPrintCtrl1.Rtf);
                richTextBoxPrintCtrl1.Modified = false;
                sw.Close();
                lastsave.Text = "Last Saved: " + DateTime.Now.ToString();

            }
            catch (Exception exc)
            {
                MessageBox.Show("Failed to save file. \n \n" + exc.Message);
            }
            finally
            {
                if (sw != null) sw.Close();
            }

And SaveFileAs

private void SaveFileAs()
    {
        SaveFileDialog sfdSaveFile = new SaveFileDialog();//Creates a new instance of the SaveFileDialog
        sfdSaveFile.Title = "Save File";//The title of the SaveFileDialog window
        sfdSaveFile.FileName = "Untitled";//The default filename in the SaveFileDialog window
        sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt";//The supported file extensions when saving
        if (sfdSaveFile.ShowDialog() == DialogResult.OK)//If the condition is correct, run the lines of code
            try//try to run the code
            {
                filepath = sfdSaveFile.FileName;//get the filepath of the file once it is saved
                SaveFile();//Calls the SaveFile object
                this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(sfdSaveFile.FileName));//Set the form name
                lastsave.Text = "Last Saved: " + DateTime.Now.ToString();//Writes the text to the lastsave.Text label, followed by the current date and time
                richTextBoxPrintCtrl1.Modified = false;
                return;
            }
            catch (Exception exc)//Catches any errors
            {
              MessageBox.Show("An error occured whilst saving. " + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
                else if (sfdSaveFile.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

        else if (sfdSaveFile.ShowDialog() == DialogResult.Cancel)//If the condition is true, run the line of code
        {
            return;
        }

If anyone could help me determine why this is occurring, I'd really appreciate it..

--EDIT--

I forgot to mention that if the user does go through and save the file, the SaveFileDialog doesn't open up another SaveFileDialog. It is something to do with cancelling the SaveFileDialog which causes the issue.

Était-ce utile?

La solution

sfdSaveFile.ShowDialog() opens the file dialog. If it's not DialogResult.OK the first time, it goes to the else clause and gets called again. Store the result of ShowDialog and check what it is, don't call it every time.

In order to do so, use this sort of if/else:

DialogResult dialogResult = sfdSaveFile.ShowDialog();
if (dialogResult == DialogResult.OK)
{
}
else if (dialogResult == DialogResult.Cancel)
{
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top