Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Display Modes
  #1 (permalink)  
Old July 23rd, 2012, 07:40 PM
Authorized User
Points: 54, Level: 1
Points: 54, Level: 1 Points: 54, Level: 1 Points: 54, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 07:45 PM.
 


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
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 05: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 02:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 05:56 AM



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


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