Results 1 to 3 of 3

Thread: [RESOLVED] How to refer to the parent method?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    66

    Resolved [RESOLVED] How to refer to the parent method?

    Code:
            private void btn_OnTop_Click(object sender, EventArgs e)
            {
                this.TopMost = (this.TopMost == false) ? true : false ;
                // how to refer to btn_OnTop?
                btn_OnTop.Text = (this.TopMost == false) ? "On Top" : "Not On Top" ;
            }
    Is there a keyword like "this" to refer to btn_OnTop itself inside btn_OnTop?
    Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to refer to the parent method?

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2012
    Posts
    66

    Re: How to refer to the parent method?

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width