I'm trying to make a mouse down event inside of a void, however all my attempts were unsuccessful, here's what I have now:
void newEllipse
{
Ellipse pic = new Ellipse()
ImageBrush img = new ImageBrush();
img.ImageSource = new BitmapImage(new Uri(gamePic));
pic.Fill = img;
pic.MouseDown += new MouseButtonEventHandler(object _, MouseButtonEventHandler e)
{
//Stuff here
};
}
However this isn't working and I'm not sure what else to do. Anyone help?
First and foremost, your question isn't clear and doesn't make much sense as pointed out in the comments. The terminology is wrong in what you are actually doing, but isn't a great deal, it needs to align with what you're actually doing.
For example as @quaabaam mentioned in the comments:
How to attach an event to a dynamically generated object
Now that's out of the way, what you are trying to do is add a event handler to that newly created object pic
. There are a few way's this can be done, but I will list an easy one that will handle that event.
pic.MouseDown += (sender, e) => { //Stuff here }
This is a lambda expression that results in a delegate function. sender
is the actual object and e
is the actual event args. Both parameters are required by the handler and must be included even though you may not use them.
The side effect of using the above is if and when you need to remove the handler, it may leave the original one there and possibly create a leak as it doesn't retain a reference to that delegate.