Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2014, 06:34 PM
Registered User
 
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);
        }
    }
 
Old February 10th, 2014, 06:50 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
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!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4, Exercise 2 Solution malie22001 BOOK: Beginning Visual C# 2010 0 May 19th, 2012 11:43 PM
Chapter 14 Exercise 3 and solution. UncleJeff BOOK: Beginning ASP.NET 4 : in C# and VB 2 March 14th, 2011 02:15 PM
Chapter 3 Exercise 4 Solution KurtBergman BOOK: Beginning Visual C# 2010 8 July 16th, 2010 08: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 07:16 AM
Solution to exercise 7, Chapter 2 Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 May 28th, 2006 04:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.