如何使它在用户单击取消时取消对话框

本文关键字:取消 对话框 单击 用户 何使它 | 更新日期: 2023-09-27 18:07:14

我有以下代码:

打开文件代码

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)
            {
            }

SaveFile代码

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

如果show对话框不是yes,则会发生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;
}

现在,在Open和New file方法中执行以下操作(假设这些是方法头)

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

问题就在这里:

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

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