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

Tob*_*oby -1 c# save .net-4.0 richtextbox savefiledialog

我的程序中有一个SaveFileDialog.问题是当我在对话框上单击"取消"时,另一个SaveFileDialog打开.但是,当我点击取消第二SaveFileDialog,第三确实出现,所以它不是一个圈或类似的东西.我无法看到是什么导致我的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();
            }
Run Code Online (Sandbox Code Playgroud)

和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;
        }
Run Code Online (Sandbox Code Playgroud)

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

- 编辑 -

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

Mik*_*cup 7

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

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

DialogResult dialogResult = sfdSaveFile.ShowDialog();
if (dialogResult == DialogResult.OK)
{
}
else if (dialogResult == DialogResult.Cancel)
{
}
Run Code Online (Sandbox Code Playgroud)