Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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 July 23rd, 2012, 06:40 PM
Authorized User
 
Join Date: Jul 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Trying to make a generic get() method (or "Can you typecast a control?")

I have an object of type Form, called Form1. Then I have another object of type MyClass (a class I created) called Object1. I'm trying to write a get() method within Form1 that will allows me to get the value of most any control within it.

I'm guessing there's a more elegant way to do this, but here's what I've got so far:

Code:
        public string get(string control_name) {
            foreach (Control ctrl in this.Controls) {

                // If this is the right control
                if (ctrl.Name == control_name) {
                    return returnValue(ctrl);

                // Look inside container elements
                } else if (ctrl.Controls.Count > 0) {
                    string tmp = getDeeper(ctrl, control_name);

                    // If tmp isn't "" then we found control_name and it wasn't "".  
                    // Note that if we did find control_name but it's value was "" 
                    // then we'd keep looking for it as if we haven't found it yet.
                    // This would cause the function to take longer but the final
                    //  result is the same.
                    if (tmp != "") return tmp;
                }
            }
            return "";
            
        }

        private string getDeeper(Control this_control, string control_name) {
            foreach (Control ctrl in this_control.Controls) {

                // If this is the right control
                if (ctrl.Name == control_name) {
                    return returnValue(ctrl);

                // Look inside container elements
                } else if (ctrl.Controls.Count > 0) {
                    string tmp = getDeeper(ctrl, control_name);

                    //  See the comment above in the get() method
                    if (tmp != "") return tmp;
                }
            }
            return "";
        }
        

        private string returnValue(Control this_control) {

            // If it's a textbox then just return the .Text() 
            if (this_control.ToString().StartsWith("System.Windows.Forms.TextBox")) {
                return this_control.Text;

	    // Combo box
            } else if (this_control.ToString().StartsWith("System.Windows.Forms.ComboBox")) {
                return this_control.Text;

	    // Radio button
            } else if (this_control.ToString().StartsWith("System.Windows.Forms.RadioButton")) {
                return this_control.Checked.ToString();
            } else {
                return "";
            } 
        }
So the error (shown in red above) is due to the fact that this_control is being treated as a generic control, however the if statement that leads me to that offending line guarantees that the control is indeed a radio button (and therefore should have a Checked() method).

Is there a way for me to "typecast" this_control as a RadioButton so that the compiler is happy?

OR am I being obtuse and there's some magical way of making a generic get() method using native C# tools?

Last edited by KaneT; July 23rd, 2012 at 06:45 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling the "Regsiter" action method using AJAX (page 122) lagonave BOOK: Professional ASP.NET MVC 2 24 December 12th, 2012 08:37 AM
p. 599 - "The name of the preceding method is doSomething:withAnotherPara" ZeroFill BOOK: Beginning iOS 4 Application Development 0 November 1st, 2011 04:50 PM
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM





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