Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Get what object a button belongs to?

Get what object a button belongs to?

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    I will try to shorten this question. If I have the following class:

    public class MyClass
    {
    public int MyInt {get; set;}
    public Button MyBtn {get; set;}
    }

    Now, let's say I have a for-loop that generates several objects of this class and hooks it up to an event handler like this:

    for (int i = 0; i < 5; i++)
    {
    MyClass myClass = new MyClass();
    myClass.MyInt = i;
    myClass.MyBtn = new Button();
    myClass.MyBtn.Click += (s, e) =>
    {
    //Is it possible to get the value of 'myClass.MyInt'
    //in this event handler since 'myClass.MyBtn' belongs
    //to the same object?
    };
    }

    So, my question is: Is it possible to get the value of 'myClass.MyInt' when the button is clicked? I mean, 'myClass.MyBtn' and 'myClass.MyInt' belongs to the same object. The problem is that I must hook up all buttons to the same event handler. Thanks for help!

    P L L D 4 Replies Last reply
    0
    • X xkrja

      I will try to shorten this question. If I have the following class:

      public class MyClass
      {
      public int MyInt {get; set;}
      public Button MyBtn {get; set;}
      }

      Now, let's say I have a for-loop that generates several objects of this class and hooks it up to an event handler like this:

      for (int i = 0; i < 5; i++)
      {
      MyClass myClass = new MyClass();
      myClass.MyInt = i;
      myClass.MyBtn = new Button();
      myClass.MyBtn.Click += (s, e) =>
      {
      //Is it possible to get the value of 'myClass.MyInt'
      //in this event handler since 'myClass.MyBtn' belongs
      //to the same object?
      };
      }

      So, my question is: Is it possible to get the value of 'myClass.MyInt' when the button is clicked? I mean, 'myClass.MyBtn' and 'myClass.MyInt' belongs to the same object. The problem is that I must hook up all buttons to the same event handler. Thanks for help!

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      If you look at the method declaration for your event handler, you'll see a parameter called s of type object - by convention, this is the object that raised the event. So, all you need do is cast s to Button as in:

      Button button = s as Button;
      if (button != null)
      {
      // Do something...
      }

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      X 1 Reply Last reply
      0
      • X xkrja

        I will try to shorten this question. If I have the following class:

        public class MyClass
        {
        public int MyInt {get; set;}
        public Button MyBtn {get; set;}
        }

        Now, let's say I have a for-loop that generates several objects of this class and hooks it up to an event handler like this:

        for (int i = 0; i < 5; i++)
        {
        MyClass myClass = new MyClass();
        myClass.MyInt = i;
        myClass.MyBtn = new Button();
        myClass.MyBtn.Click += (s, e) =>
        {
        //Is it possible to get the value of 'myClass.MyInt'
        //in this event handler since 'myClass.MyBtn' belongs
        //to the same object?
        };
        }

        So, my question is: Is it possible to get the value of 'myClass.MyInt' when the button is clicked? I mean, 'myClass.MyBtn' and 'myClass.MyInt' belongs to the same object. The problem is that I must hook up all buttons to the same event handler. Thanks for help!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Yes. There are 5 MyClass instances and you can use one method for all events. This is better :

        private List<MyButton> myButtons = new List<MyButton>();
        private void CreateButtons()
        {
        for(int i = 0; i < 5; i++)
        {
        MyButton myButton = new MyButton();
        myButton.MyInt = i;
        myButton.Click += new EventHandler(myButton_Click);
        myButtons.Add(myButton);
        }
        }

        private void myButton_Click(object sender, EventArgs e)
        {
        MyButton myButton = sender as MyButton;
        if(myButton != null)
        {
        MessageBox.Show(myButton.MyInt.ToString());
        }
        }

        public class MyButton : Button
        {
        private int myInt;

        public int MyInt
        {
        get { return myInt; }
        set { myInt = value; }
        }
        }

        X 1 Reply Last reply
        0
        • P Pete OHanlon

          If you look at the method declaration for your event handler, you'll see a parameter called s of type object - by convention, this is the object that raised the event. So, all you need do is cast s to Button as in:

          Button button = s as Button;
          if (button != null)
          {
          // Do something...
          }

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          X Offline
          X Offline
          xkrja
          wrote on last edited by
          #4

          Thanks for the reply. Yes, I know I can get the object that raised that event. But that object belongs to an object 'myClass' which in turn contains 'myClass.MyInt'. Is it possible to get which of the 'myClass.MyBtn' that raised the event. If I can get that I would also be able to read out the value of 'myClass.MyInt'

          1 Reply Last reply
          0
          • L Lost User

            Yes. There are 5 MyClass instances and you can use one method for all events. This is better :

            private List<MyButton> myButtons = new List<MyButton>();
            private void CreateButtons()
            {
            for(int i = 0; i < 5; i++)
            {
            MyButton myButton = new MyButton();
            myButton.MyInt = i;
            myButton.Click += new EventHandler(myButton_Click);
            myButtons.Add(myButton);
            }
            }

            private void myButton_Click(object sender, EventArgs e)
            {
            MyButton myButton = sender as MyButton;
            if(myButton != null)
            {
            MessageBox.Show(myButton.MyInt.ToString());
            }
            }

            public class MyButton : Button
            {
            private int myInt;

            public int MyInt
            {
            get { return myInt; }
            set { myInt = value; }
            }
            }

            X Offline
            X Offline
            xkrja
            wrote on last edited by
            #5

            Thanks for the reply! I think that was exactly what I was looking for. I'll try to implement this in my real case and I get back if I don't succeed.

            1 Reply Last reply
            0
            • X xkrja

              I will try to shorten this question. If I have the following class:

              public class MyClass
              {
              public int MyInt {get; set;}
              public Button MyBtn {get; set;}
              }

              Now, let's say I have a for-loop that generates several objects of this class and hooks it up to an event handler like this:

              for (int i = 0; i < 5; i++)
              {
              MyClass myClass = new MyClass();
              myClass.MyInt = i;
              myClass.MyBtn = new Button();
              myClass.MyBtn.Click += (s, e) =>
              {
              //Is it possible to get the value of 'myClass.MyInt'
              //in this event handler since 'myClass.MyBtn' belongs
              //to the same object?
              };
              }

              So, my question is: Is it possible to get the value of 'myClass.MyInt' when the button is clicked? I mean, 'myClass.MyBtn' and 'myClass.MyInt' belongs to the same object. The problem is that I must hook up all buttons to the same event handler. Thanks for help!

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              You don't even need an extra class, you could store the MyClass reference inside the Button using myClass.MyBtn.Tag=myClass; so later an event handler could convert its sender to Button to MyClass instance. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              1 Reply Last reply
              0
              • X xkrja

                I will try to shorten this question. If I have the following class:

                public class MyClass
                {
                public int MyInt {get; set;}
                public Button MyBtn {get; set;}
                }

                Now, let's say I have a for-loop that generates several objects of this class and hooks it up to an event handler like this:

                for (int i = 0; i < 5; i++)
                {
                MyClass myClass = new MyClass();
                myClass.MyInt = i;
                myClass.MyBtn = new Button();
                myClass.MyBtn.Click += (s, e) =>
                {
                //Is it possible to get the value of 'myClass.MyInt'
                //in this event handler since 'myClass.MyBtn' belongs
                //to the same object?
                };
                }

                So, my question is: Is it possible to get the value of 'myClass.MyInt' when the button is clicked? I mean, 'myClass.MyBtn' and 'myClass.MyInt' belongs to the same object. The problem is that I must hook up all buttons to the same event handler. Thanks for help!

                D Offline
                D Offline
                Daniel Grunwald
                wrote on last edited by
                #7

                xkrja wrote:

                //Is it possible to get the value of 'myClass.MyInt' //in this event handler since 'myClass.MyBtn' belongs //to the same object?

                In your example code: yes. Just use myClass.MyInt. The lambda expression will capture the myClass variable, so every event handler will know to which MyClass instance it belongs. You should not capture the i variable - that would always be 5 because the loop has terminated when the event executes. But capturing variables declared inside the loop will give you the value from the corresponding iteration.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups