Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 23rd, 2012, 05:19 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Lesson 18 Exercise 3

Hi,

I was comparing my code to that in your solution, and instead of declaring the 'shouldContinue' variable, I used the following:

Code:
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Decides whether we should continue with this operation.
            e.Cancel = true;

            // See if we have unsaved changes.
            if (contentRichTextBox.Modified)
            {
                // Ask the user if we should save the changes.
                DialogResult result =
                    MessageBox.Show(
                        "There are unsaved changes.\n" +
                        "Do you want to save the changes?",
                        "Save Changes?",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Question);

                // See if we should save the changes.
                if (result == DialogResult.Yes)
                {
                    // Save the changes here.
                    contentRichTextBox.SaveFile("Test.rtf", RichTextBoxStreamType.RichText);

                    // It's now safe to continue.
                    e.Cancel = false;
                }
                else if (result == DialogResult.No)
                {
                    // Just continue and that will discard the changes.
                    e.Cancel = false;
                }
                else
                {
                    // The user wants to cancel.
                    // Don't continue.
                    e.Cancel = true;
                }
            }
            else
            {
                // No unsaved changes so it's safe to continue.
                e.Cancel = false;
            }

        }
Is there anything wrong with using e.Cancel directly for the if/else tests rather than the shouldContinue variable, or is it simply a case of your code being more explicit as to what it's doing? I'm trying to learn best practice here, so I'd appreciate your advice.
  #2 (permalink)  
Old November 23rd, 2012, 12:23 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Just out of habit I usually only set e.Cancel once and only if I need to. If I want it to take the default value, I leave it alone.

But there's absolutely nothing wrong with the way you're doing it. In fact, now that I think about it, I'm not sure why I have this habit.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
SamW (November 28th, 2012)
  #3 (permalink)  
Old November 28th, 2012, 07:51 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Rod! I know everyone has their own habits, that's the challenge for me learning - I read through other people's code and there are numerous ways of doing the same thing so I get confused. I should probably drop the habit of wanting to be Right, in favour of just making sure my code is clear and has comments as needed.

EDIT:

Aha! Now I see the value of your way! When you come to do Exercise 20.4, pasting my version into an IsDataSafe method throws an error because e hasn't been declared.

Last edited by SamW; November 28th, 2012 at 10:22 AM..
  #4 (permalink)  
Old November 28th, 2012, 12:15 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

You're right that for any given problem there are many solutions. You can try to find the "best" solution but at some point you need to decide when a solution is good enough and stop there. Otherwise you can tweak the code endlessly and never finish. And does it really matter if a program takes 10 milliseconds or 9 to accomplish a task?

I usually favor readability over performance. If the code is 10% slower than the perfect solution but the faster solution is to complicated to debug and maintain, then the slower solution is better.

Often that also means going with what you're used to. Over time you develop habits and if you stick with them you're more likely to not make mistakes.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #5 (permalink)  
Old December 6th, 2012, 07:24 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Save As Dialog

I have modified the code for the yes result of the dialog box so that instead of saving to a hard coded test.rtf file, it shows the save as dialog.

I used the code that i used for the Save As menu option.

I have a problem though. The dialog shows ok if I want to save the modifications, I click yes and it shows the save as dialog box, however, if after showing the dialog box i click cancel, it crashes. It has an error ArgumentException was unhandled - Empty pathname is not legal. Below is the code i am using. The red text is the line where the error crashes to.

Code:
private void SimpleEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //decides whether to continue with the operation
            bool shouldContinue;

            // Check for unsaved changes.
            if (contentRichTextBox.Modified)
            {
                // Ask the user if we should save the changes.
                DialogResult result =
                    MessageBox.Show(
                        "There are unsaved changes.\n" +
                        "Do you want to save the changes?",
                        "Save Changes?",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Question);

                // See if we should save the changes.
                if (result == DialogResult.Yes)
                {
                    // Save the changes.
                    saveFileDialog1.ShowDialog();
                    contentRichTextBox.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
                    openFileDialog1.FilterIndex = saveFileDialog1.FilterIndex;

                    // It's now safe to continue.
                    shouldContinue = true;
                }
                else if (result == DialogResult.No)
                {
                    // continue and discard the changes.
                    shouldContinue = true;
                }
                else
                {
                    // The user wants to cancel.
                    // Don't continue.
                    shouldContinue = false;
                }
            }
            else
            {
                // No unsaved changes so it's safe to continue.
                shouldContinue = true;
            }

            // See if it's safe to continue.
            e.Cancel = (!shouldContinue);
        }
I figure that it means that it wants a filename, but why when i cancel the save as dialog isnt it just cancelling? Is it because its still part of the save modifications dialog box? Do i need a new 'if' statement to cover the save as dialog?
__________________
"These pretzels are making me thirsty"
  #6 (permalink)  
Old December 7th, 2012, 04:04 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I think this is the key piece of code:

Code:
if (result == DialogResult.Yes)
{
    // Save the changes.
    saveFileDialog1.ShowDialog();
    contentRichTextBox.SaveFile(saveFileDialog1.FileName,
        RichTextBoxStreamType.RichText);
    openFileDialog1.FilterIndex = saveFileDialog1.FilterIndex;

    // It's now safe to continue.
    shouldContinue = true;
}
You displayed the "Save Changes?" dialog (not shown in this code) and the user clicked Yes. Then the red code above displays the Save File dialog and the user clicks Cancel. The red code needs to check the Save File dialog's return result to see if the user clicked Cancel.

I think it might look something like this:

Code:
if (result == DialogResult.Yes)
{
    // Save the changes.
    if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)
    {
        // Cancel everything.
        shouldContinue = false;
    }
    else
    {
        contentRichTextBox.SaveFile(saveFileDialog1.FileName,
            RichTextBoxStreamType.RichText);
        openFileDialog1.FilterIndex = saveFileDialog1.FilterIndex;

        // It's now safe to continue.
        shouldContinue = true;
    }
}
I'm typing without trying this in Visual Studio so I may have the details wrong but I think this is the sort of thing you'll need to do.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #7 (permalink)  
Old December 11th, 2012, 12:55 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I will try it out and let you know
__________________
"These pretzels are making me thirsty"
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 4 Try it Exercise caa5042 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 13 February 2nd, 2013 03:32 PM
Lesson 4 Exercise 2 Rennuke BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 3 January 17th, 2013 08:49 PM
Lesson 13 Exercise 3 1rf2aa3oa4tb BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 July 2nd, 2012 12:04 PM
Lesson 18 - TryIt sendalot BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 June 6th, 2012 10:30 AM
Lesson 19 Exercise 4 mpthemaster BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 June 11th, 2011 08:54 PM



All times are GMT -4. The time now is 06:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.