如果取消,为什么我的SaveFileDialog会再次显示?

时间:2013-05-11 22:18:54

标签: c# .net-4.0 save richtextbox savefiledialog

我的程序中有一个SaveFileDialog。问题是当我在对话框上单击“取消”时,另一个SaveFileDialog打开。但是当我在第二个SaveFileDialog上单击取消时,第三个会出现 NOT ,所以它不是循环或类似的东西。我无法看到是什么导致我的SaveFileDialog以如此奇怪的方式表现。显然我需要修复它,以便如果用户在第一个SaveFileDialog上单击取消,它会将它们返回到表单。

我的程序中保存的代码如下:

 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();
            }

和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;
        }

如果有人能帮我确定为什么会这样,我真的很感激..

- 编辑 -

我忘了提到如果用户确实经历并保存文件,则SaveFileDialog不会打开另一个SaveFileDialog。这与取消导致问题的SaveFileDialog有关。

1 个答案:

答案 0 :(得分:7)

sfdSaveFile.ShowDialog()打开文件对话框。如果第一次不是DialogResult.OK,它会进入else子句并再次被调用。存储ShowDialog的结果并检查它是什么,不要每次都调用它。

为此,请使用此类if / else:

DialogResult dialogResult = sfdSaveFile.ShowDialog();
if (dialogResult == DialogResult.OK)
{
}
else if (dialogResult == DialogResult.Cancel)
{
}