|
-
May 1st, 2013, 05:18 AM
#3
Thread Starter
Lively Member
Re: How to refer to the parent method?
 Originally Posted by jmcilhinney
It's not inside btn_OnTop. It's inside the Click event handler for btn_OnTop, which is the btn_OnTop_Click method.
If you want to refer to the object that raised the event inside an event handler then you use the 'sender' parameter. In this particular case though, there's really no need to because you know for a fact that it will be btn_OnTop so you can just use the that member variable. For handlers of events for multiple objects or dynamically created objects or when the handler is not in the form containing the control though, using the 'sender' may be the only way.
By the way, your code is much more complex than it needs to be. This:
Code:
btn_OnTop.Text = (this.TopMost == false) ? "On Top" : "Not On Top" ;
would be better written like this:
Code:
btn_OnTop.Text = this.TopMost ? "Not On Top" : "On Top";
but even moreso, this:
Code:
this.TopMost = (this.TopMost == false) ? true : false ;
could be written like this:
Code:
this.TopMost = !this.TopMost
Not only did you answer my question,
but gave me more input as well.
Always good to make the codes cleaner.
Thank you.
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
|