471,354 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Obtain Sender

how do i convert a sender into a Button type of component?
im doing so becoz i wanted to access some of the properties of the button
Feb 2 '06 #1
9 1339
Hello, nagen!

n> how do i convert a sender into a Button type of component?
n> im doing so becoz i wanted to access some of the properties of the
n> button

if you mean eventhandler parameter then
object sender;

Button button = sender as Button;
if ( button != null )
{
//access properties of the button
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 2 '06 #2
nagen wrote:
how do i convert a sender into a Button type of component?
im doing so becoz i wanted to access some of the properties of the button


if( sender is Button )
{
Button b = (Button) sender ;
}
else if( sender is SomethingElse )
{
SomethingElse se = (SomethingElse) sender ;
}
Feb 2 '06 #3
Hello, #!

C> nagen wrote:
??>> how do i convert a sender into a Button type of component?
??>> im doing so becoz i wanted to access some of the properties of the
??>> button

C> if( sender is Button )
C> {
C> Button b = (Button) sender ;
C> }
C> else if( sender is SomethingElse )
C> {
C> SomethingElse se = (SomethingElse) sender ;
C> }

This cast can introduce InvalidCastException if sender is of other type

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 2 '06 #4
Vadym Stetsyak wrote:
Hello, #!

C> nagen wrote:
??>> how do i convert a sender into a Button type of component?
??>> im doing so becoz i wanted to access some of the properties of the
??>> button

C> if( sender is Button )
C> {
C> Button b = (Button) sender ;
C> }
C> else if( sender is SomethingElse )
C> {
C> SomethingElse se = (SomethingElse) sender ;
C> }

This cast can introduce InvalidCastException if sender is of other type


Thank you for your correction.

Could you please, bring me more explanation :

The InvalidCastException should only occurs later in the explicits cast "Obj o2
= (Type) o1".
And it is protected by the "if( obj is Type)".

Isn't it ?

Cyrille
Feb 2 '06 #5
C> Thank you for your correction.

C> Could you please, bring me more explanation :

C> The InvalidCastException should only occurs later in the explicits cast
C> "Obj o2 = (Type) o1".
C> And it is protected by the "if( obj is Type)".

C> Isn't it ?

Yes, I agree with you. Somehow I've missed _is_ statement :8-)
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 2 '06 #6
Vadym Stetsyak wrote:
C> Thank you for your correction.

C> Could you please, bring me more explanation :

C> The InvalidCastException should only occurs later in the explicits cast
C> "Obj o2 = (Type) o1".
C> And it is protected by the "if( obj is Type)".

C> Isn't it ?

Yes, I agree with you. Somehow I've missed _is_ statement :8-)
Ouf! My code is not so bad ;o)

By the way ... I like the "if/else" cascading like that I can handle differents
senders with the same method.

cyrille
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Feb 2 '06 #7
Sorry im a little blur here could u give me the full working code...
with the correction

thank you

"# Cyrille37 #" <cy*******@free.fr> wrote in message
news:uB**************@TK2MSFTNGP15.phx.gbl...
Vadym Stetsyak wrote:
C> Thank you for your correction.

C> Could you please, bring me more explanation :

C> The InvalidCastException should only occurs later in the explicits
cast C> "Obj o2 = (Type) o1".
C> And it is protected by the "if( obj is Type)".

C> Isn't it ?

Yes, I agree with you. Somehow I've missed _is_ statement :8-)


Ouf! My code is not so bad ;o)

By the way ... I like the "if/else" cascading like that I can handle
differents senders with the same method.

cyrille
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Feb 3 '06 #8
What they are saying is to code it like this:

if( sender is Button )
{
Button b = (Button) sender ;
b.Text = "I have been clicked!";
}
This IF statement will only be entered into if the sender really is a
button, and not something else.
(Because sender is passed as type 'object' so it really could be
anything)

Feb 3 '06 #9
thank you everyone :-)

"Steven Nagy" <le*********@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
What they are saying is to code it like this:

if( sender is Button )
{
Button b = (Button) sender ;
b.Text = "I have been clicked!";
}
This IF statement will only be entered into if the sender really is a
button, and not something else.
(Because sender is passed as type 'object' so it really could be
anything)

Feb 4 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jeronimo Bertran | last post: by
5 posts views Thread by Kevin Myers | last post: by
2 posts views Thread by Rodrigo C. Souza | last post: by
5 posts views Thread by OneFang | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.