1

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Either you have braces in places that don't need braces or you're missing lots of code. Have you stepped through it with the debugger? What do you mean by the "program skips the NewFile event"? Is the event not called or does it think .Modified is false? Getting back to those pesky braces, they don't line up at all with your if statements. – NotMe Apr 04 '13 at 00:20
  • I'm new to this.. Need stuff explained.. Sorry. –  Apr 04 '13 at 00:21
  • Yeah, it is quite likely those braces are causing, if not the issue you're concerned with, then some other one. – Steven Marciano Apr 04 '13 at 00:24
  • 2
    @Toby: Edit your post and put the entire NewFile event, including the method declaration in it. What you have now doesn't make sense. – NotMe Apr 04 '13 at 00:26
  • We've all been there my friend, just keep practicing and it will become second nature sooner than you think. But back to the issue at hand... – Steven Marciano Apr 04 '13 at 00:26
  • Added the code. Please see original question. –  Apr 04 '13 at 00:30
  • It's working now that I removed the "return". Thanks! –  Apr 04 '13 at 00:34

2 Answers2

6

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;
}
NotMe
  • 87,343
  • 27
  • 171
  • 245
  • You sir are a genius! That works so well! Thanks mate. I know for the future now. I'm learning a lot about this programming stuff and it's rather enjoyable. I don't suppose you'd know how I can implement an undo/redo into my program? Just so I can undo and redo text typed in a richTextBox. –  Apr 04 '13 at 00:56
  • @Toby you already asked and accepted answer about undo/redo here http://stackoverflow.com/questions/15772602/how-to-undo-and-redo-in-c-sharp-rich-text-box – Julián Urbano Apr 04 '13 at 00:58
  • That was for the undo only. I need the redo also. –  Apr 04 '13 at 01:00
  • 1
    @Toby Then do the undo the other way around? :) – SeToY Apr 04 '13 at 06:56
  • Derp. Cheers bud. Didn't think of that. –  Apr 04 '13 at 15:54
  • @user2234123: I think you need to set a break point at the top of your methods and step through the application to see what's going on. – NotMe Apr 05 '13 at 13:56
2

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.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52