handling mouse events in runtime created objects in c#

1.1k Views Asked by At

I'm writing a c# windows app, and as one task, I am creating Panel objects at runtime. I have my custom Panel which is defined as:

class FlowState : Panel
{
:
:
}

I have an init method to set size, location, etc. However once this panel is created on the windows form, I want to handle mouse events, such as mouseDown and mouseUp. If you created a panel at design time and used the gui to define these events, you would get methods like the following (for a panel named 'panel1'):

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    //do stuff
}

How do I put code into my FlowState object which extends Panel to handle mouse events such as this?

1

There are 1 best solutions below

2
On BEST ANSWER

you can attach the event like this...

private void CreatePanel()
}
    var panel = new FlowState();
    panel.MouseDown += new MouseEventHandler(MouseDown);
}

private void MouseDown(object sender, MouseEventArgs e)
{ 
}