【问题标题】:Why does my SaveFileDialog display again if cancelled?如果取消,为什么我的 SaveFileDialog 会再次显示?
【发布时间】:2013-05-11 22:18:54
【问题描述】:

我的程序中有一个 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();
            }

然后将文件另存为

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 有关。

【问题讨论】:

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


    【解决方案1】:

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

    为此,请使用这种 if/else:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 2014-12-13
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      相关资源
      最近更新 更多