【问题标题】:How do I make it so that when the user clicks cancel, it cancels the dialog?如何做到这一点,以便当用户单击取消时,它会取消对话框?
【发布时间】:2013-04-04 00:15:23
【问题描述】:

我有以下代码:

打开文件代码

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

将文件另存为代码

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 事件(即使文本已被修改)。我怎样才能做到这一点,当我单击“打开”时,它会询问我是否要保存(如果文本被修改)。然后如果我点击取消,它会返回到表单?

对不起。我对编程真的很陌生,所以这都是一个学习曲线。

【问题讨论】:

  • 要么你在不需要大括号的地方有大括号,要么你缺少很多代码。您是否使用调试器逐步完成了它? “程序跳过 NewFile 事件”是什么意思?事件是没有被调用还是它认为 .Modified 是假的?回到那些讨厌的大括号,它们根本不符合您的 if 语句。
  • 我是新手。需要解释一下。对不起。
  • 是的,很可能是这些大括号引起的,如果不是您关心的问题,那么可能是其他问题。
  • @Toby:编辑您的帖子并将整个 NewFile 事件,包括其中的方法声明。你现在拥有的东西没有意义。
  • 朋友,我们都去过那里,只要继续练习,它就会比你想象的更快成为第二天性。但回到手头的问题......

标签: c# winforms visual-studio-2012 openfiledialog


【解决方案1】:

好的,我想我明白这里发生了什么。首先,我不相信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;
}

【讨论】:

  • 您先生是个天才!效果很好!谢了哥们。我现在知道未来。我学到了很多关于这个编程的东西,而且很有趣。我想你不知道我如何在我的程序中实现撤消/重做?这样我就可以撤消和重做在richTextBox 中输入的文本。
  • @Toby 您已经在此处询问并接受了有关撤消/重做的答案stackoverflow.com/questions/15772602/…
  • 那只是为了撤消。我也需要重做。
  • @Toby 然后以相反的方式撤消? :)
  • 德普。干杯芽。没想到。
【解决方案2】:

问题出在这里:

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

删除那个return。但是,正如@Chris 所说,您应该用户选择要打开的新文件之前询问是否保存当前文件。

【讨论】:

  • @Toby 看看 Chris 的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 2013-09-03
相关资源
最近更新 更多