user2234123
user2234123

Reputation:

How do I make it so that when the user clicks cancel, it cancels the dialog?

I have the following code:

Open File Code

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

New File Code

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 Code

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 Code

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

Currently, the program skips the NewFile event (even if the text has been modified). How can I make it so that when I click "Open", it asks me if I would like to save (if the text is modified). Then if I click cancel, it returns me to the form?

Sorry. I'm really new to programming so this is all a learning curve.

Upvotes: 1

Views: 204

Answers (2)

ChrisLively
ChrisLively

Reputation: 88044

Okay, I think I see what's going on here. First off I don't believe return; works the way you think it does.

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

You have a return; call that happens if the show dialog is not yes. The { newFile() } code doesn't need braces around it. So those lines are really:

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

NewFile();

Now, given your requirement, NewFile is called WAY too late in the game anyway. You want that to happen before you ask them what to open; just like most other windows programs work.

But, there's another issue. Your return statement in the NewFile method is simply returning from NewFile. It's not telling the previous method to bail out.

So the NewFile method needs a return type to indicate whether to allow the calling method to go forward or not.

And, looking at your save file you have a return method there too. What's with all of the return; calls?

Which brings us back to how do we fix this?

Answer: rewrite the whole thing. Starting with the following method:

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

Now, in your Open and New file methods do the following (assuming these are the method headers)

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

Upvotes: 6

Julián Urbano
Julián Urbano

Reputation: 8488

The problem is here:

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

remove that return. But, as @Chris says, you should ask whether to save the current file or not before the user selects the new file to open.

Upvotes: 2

Related Questions