Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 10th, 2014, 06:34 PM
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 10 Exercise 3 Alternative Solution

Per the comment in the exercise reminding us that we had already written a colorsamples selector in an earlier lesson, I wrote a version of the above exercise by adding a reference to the earlier project rather than copy and past it. This code is not perfect but I thought I would share it.

I made the sampleColorLabel public and encapsulated the code to call the dialog in a helper method which references the Namespace of the previous project.

Code:
        //Current foreground or background color passed in.
        private Color displayDialog(Color color)
        {
            ColorSamples.ColorSamples colorSamplesDialog;
            colorSamplesDialog = new ColorSamples.ColorSamples();

            
            if (colorSamplesDialog.ShowDialog() == DialogResult.OK)
            {
                //If OK is selected return the new color
                return colorSamplesDialog.sampleLabel.BackColor;
            }

            else
            {
                //otherwise return the same color passed in (no change in color).
                return color;
            }
        }
I then call this helper method from each of the buttons:

Code:
       private void foreColorButton_Click(object sender, EventArgs e)
        {
            //Call dialog.  OK: new color 
            //Cancel: return the current ForeColor, resulting in no change.
            this.ForeColor = displayDialog(this.ForeColor);
        }

        private void backColorButton_Click(object sender, EventArgs e)
        {
            //Call dialog.  OK: new color
            //Cancel: return the current ForeColor, resulting in no change.
            this.BackColor = displayDialog(this.BackColor);
        }
    }
Reply With Quote
  #2 (permalink)  
Old February 10th, 2014, 06:50 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,141, Level: 23
Points: 3,141, Level: 23 Points: 3,141, Level: 23 Points: 3,141, Level: 23
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 640
Thanks: 2
Thanked 96 Times in 95 Posts
Default

That's a good solution.

More generally, if you have a method such as this one that you plan to reuse in many solutions, you should put it in a class library project. Then you can add references to that project to the others and use the method in the library much as you did here.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
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
Chapter 4, Exercise 2 Solution malie22001 BOOK: Beginning Visual C# 2010 0 May 20th, 2012 12:43 AM
Chapter 14 Exercise 3 and solution. UncleJeff BOOK: Beginning ASP.NET 4 : in C# and VB 2 March 14th, 2011 03:15 PM
Chapter 3 Exercise 4 Solution KurtBergman BOOK: Beginning Visual C# 2010 8 July 16th, 2010 09:13 PM
Chapter 13 Exercise 3 solution firblazer BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 19th, 2008 08:16 AM
Solution to exercise 7, Chapter 2 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 May 28th, 2006 05:28 AM



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


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