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 24th, 2012, 02:31 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 How do I go from generic 'object' to more specific object?

I have a TextBox named textbox_input which calls the following function when focus is lost:

Code:
private void textbox_input_Leave(object sender, EventArgs e) {
    textbox_debug.Text += "GetType: " + sender.GetType().ToString();
    textbox_debug.Text += "ToString: " + sender.ToString();
    //MyOtherObject.testFormControl (sender);
}
Typing "Hello" into the TextBox and leaving prints out:

Quote:
GetType: System.Windows.Forms.TextBox
ToString: System.Windows.Forms.TextBox, Text: Hello
Now what I would to do is call the (commented out) testFormControl method located in MyOtherObject. This method has the following prototype:

Code:
public void testFormControl (System.Windows.Forms.TextBox to_test)
However when I uncomment the line above I get the error:

Quote:
Argument 1: cannot convert from 'object' to 'System.Windows.Forms.TextBox'
So it seems to me that I have this generic 'object' type that references the actual TextBox object, how do I de-reference, if you will, so that my testFormControl() method can receive the actual TextBox object?
  #2 (permalink)  
Old July 24th, 2012, 03:50 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 Found the answer

I found the answer to my problem. Seems like a screwy way of doing things but it works.

The fix looks like this:

Code:
private void textbox_input_Leave(object sender, EventArgs e) {
    textbox_debug.Text += "GetType: " + sender.GetType().ToString();
    textbox_debug.Text += "ToString: " + sender.ToString();
    if (sender is System.Windows.Forms.TextBox)
                MyOtherObject.testFormControl (sender as System.Windows.Forms.TextBox);
}
The if statement isn't really necessary since I know that it was a TextBox that got me there, but I added it for completeness of this solution. Obviously the textbox_debug.Text lines are just for my own debugging and have no place in your code. :)

Credit: http://stackoverflow.com/questions/1...-forms-textbox
 


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
Chap 14 - pg 517 - Item 9 (Object reference not set to an instance of an object. ) jkoyle BOOK: Beginning ASP.NET 4 : in C# and VB 5 August 25th, 2011 08:50 AM
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM
How do I connect to a specific object? DarrenMelling VB How-To 3 November 29th, 2006 08:53 AM
Serializing Object Graph - Assigning Object to Jag venkat.kl C# 0 August 28th, 2006 10:39 AM
create a Line object ,Box object in CR at Runtime? thanhnt Pro VB 6 1 May 16th, 2005 06:51 AM



All times are GMT -4. The time now is 12:32 PM.


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