Get what object a button belongs to?
-
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!
-
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!
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.
-
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!
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; }
}
} -
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.
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'
-
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; }
}
} -
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!
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.
-
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!
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 themyClass
variable, so every event handler will know to which MyClass instance it belongs. You should not capture thei
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.