c# - 我如何做到这一点,以便用户单击“取消”时可以取消对话框?

标签 c# winforms visual-studio-2012 openfiledialog

我有以下代码:

打开文件代码

OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open File";
        ofd.FileName = "";
        ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html"; StreamReader sr = null;
        if (ofd.ShowDialog() != DialogResult.Yes) return;
        {
            NewFile();
        }
        try
        {

            sr = new StreamReader(ofd.FileName);
            this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(ofd.FileName));
            richTextBoxPrintCtrl1.Text = ofd.FileName;
            richTextBoxPrintCtrl1.Text = sr.ReadToEnd();
            filepath = ofd.FileName;
            richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);

        }
        catch
        {
        }
        finally
        {
            if (sr != null) sr.Close();
        }


新文件代码

if (richTextBoxPrintCtrl1.Modified)
        {
            DialogResult r = MessageBox.Show(this, "Save Current Document?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (r == DialogResult.Yes) SaveFile();
            if (r == DialogResult.Cancel) return;
        }
        this.Text = string.Format("Untitled - Basic Word Processor");
        richTextBoxPrintCtrl1.Text = "";
        filepath = null;
    }
    }


SaveFileAs代码

SaveFileDialog sfdSaveFile = new SaveFileDialog();
        sfdSaveFile.Title = "Save File";
        sfdSaveFile.FileName = "Untitled";
        sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
        if (sfdSaveFile.ShowDialog() == DialogResult.OK)
            try
            {
                filepath = sfdSaveFile.FileName;
                SaveFile();
                this.Text = string.Format("{0} - Basic Word Processor", Path.GetFileName(sfdSaveFile.FileName));
            }
            catch (Exception exc)
            {

            }


保存文件代码

        if (filepath == null)
        {
            SaveFileAs();
            return;

        }
        StreamWriter sw = new StreamWriter(filepath);
        //StreamWriter stwrite = null;
        try
        {

            sw.WriteLine(richTextBoxPrintCtrl1.Text);
            richTextBoxPrintCtrl1.Modified = false;
            sw.Close();

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


当前,程序将跳过NewFile事件(即使文本已被修改)。我该如何做,以便当我单击“打开”时,它询问我是否要保存(如果文本已修改)。然后,如果我单击“取消”,它将返回到表单吗?

抱歉。我真的是编程新手,所以这都是学习曲线。

最佳答案

好的,我想我知道这里发生了什么。首先,我不相信return;以您认为的方式工作。

if (ofd.ShowDialog() != DialogResult.Yes) return;
        {
            NewFile();
        }


如果显示对话框不是,您将发生一个return;调用。 { newFile() }代码不需要大括号。因此,这些行实际上是:

if (ofd.ShowDialog() != DialogResult.Yes) return;

NewFile();


现在,根据您的要求,NewFile在游戏中为时已晚。您希望在问他们打开什么之前发生这种情况。就像大多数其他Windows程序一样。

但是,还有另一个问题。您在NewFile方法中的return语句只是从NewFile返回。这并不是在告诉前一种方法纾困。

因此,NewFile方法需要一个返回类型来指示是否允许调用方法前进。

而且,查看您的保存文件,那里也有一个返回方法。所有return;调用是什么?

这使我们回到如何解决这个问题?

答:重写整个事情。从以下方法开始:

private Boolean CanClear() {
    Boolean result = false;
    if (richTextBoxPrintCtrl1.Modified)
    {
        DialogResult r = MessageBox.Show(this, "Save Current Document?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
        if (r == DialogResult.Yes) {
            SaveFile();
            result = true;
        }
    } else {
        result = true;
    }  
    return result;
}


现在,在“打开”和“新建”文件方法中执行以下操作(假设这些是方法标题)

protected void OpenFile(...) {
    if (!CanClear()) return;
    .... now execute the code to load the open dialog and the selected file.
}

protected void NewFile(...) {
    if (!CanClear()) return;

    this.Text = string.Format("Untitled - Basic Word Processor");
    richTextBoxPrintCtrl1.Text = "";
    filepath = null;
}

关于c# - 我如何做到这一点,以便用户单击“取消”时可以取消对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15800469/

相关文章:

c# - 如何使用密码在 VB.NET(或 C#)中使用 "randomize"颜色数组?

c# - 散布一个列表<>

c# - 在 ToolStripDropDownMenu 中隐藏 ImageMargin 和 CheckMargin

c++ - 为什么以下两个程序会产生不同的输出? (C++,位操作,VS 2012)

c++ - 在 Visual Studio 2012 中对 exe 中的 C++ 代码进行单元测试时阻力最小的路径

visual-studio - 解决方案资源管理器中文件上的蓝色问号

c# - 从数据库中检索基于角色的内容片段的选项

c# - foreach C# 中的字符串列表

.net - 我可以使用 C++/CLI (.NET Winforms/WPF) 为使用 native C 和 C++ 编写的应用程序提供 GUI

c# - MSChart Y 轴和 X 轴标签