|
-
Mar 9th, 2007, 08:24 AM
#1
Thread Starter
Member
Last edited by sooner; Mar 9th, 2007 at 11:45 AM.
-
Mar 9th, 2007, 10:36 AM
#2
Hyperactive Member
Re: Convert sender object to a control?
Personally, if I know that the sender is of type CheckBox then I would type cast the sender as a checkbox and check the name proprty to see which one it is. IE. ((CheckBox)sender).Name.
Currently Using: VS 2005 Professional
-
Mar 9th, 2007, 10:53 AM
#3
Re: Convert sender object to a control?
Since you haven't set the name of your controls, you could use Tag to identify your sender.
Code:
((CheckBox)sender).Tag.ToString()
-
Mar 9th, 2007, 11:38 AM
#4
Thread Starter
Member
Re: Convert sender object to a control?
Thanks guys.
I was trying to convert the sender to an object when I was trying to cast it.
-
Mar 11th, 2007, 03:35 AM
#5
Re: [RESOLVED]Convert sender object to a control?
Another way would be:
c# Code:
CheckBox cbx = sender as CheckBox;
if (cbx != null)
{
// Use cbx here.
}
The 'as' operator will attempt to cast the reference as the specified type and return null if it fails, where an explicit cast will throw an exception if it fails. It's better if there can be any doubt about the type of the object being cast. It is a good way to create more robust code.
Last edited by jmcilhinney; Mar 11th, 2007 at 03:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|