0

I am working in VS 2010, Windows Form control.

I have a extended FlowLayoutPanel, in which I dynamically add buttons

My problem is that the MouseDownEventhandler for flowlayout planel only executes when clicked anywhere except the buttons. When clicked on button the MouseDownEventHandler for the FlowLayoutPanel is not called.

I tried wiring function to the Click event handlers of the buttons added to the Panel. but I noticed delays due to which I am having issues working ahead.

Can anyone help me with this?

Vishal Patel
  • 953
  • 5
  • 11
Dinesh
  • 41
  • 6

1 Answers1

0

This is propably not the best approch but it works for me:

//global mouse down handler for controls in flow panel
private void ControlMouseDown(object sender, MouseEventArgs e)
{
    var control = (Control)sender;

    if (control.Parent is FlowLayoutPanel)
    {
        flowLayoutPanel1_MouseDown(sender, e); //if you have seperate method to handle click on flowpanel otherwise see reflection approach below
    }
}

Reflection approach:

var onMouseDown = flowLayoutPanel1.GetType().GetMethod("OnMouseDown", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
onMouseDown.Invoke(flowLayoutPanel1, new object[] { e });

You can bind global event to all child controls in flow panel easly and this works okey for me. Hope I helped :)

gzaxx
  • 17,312
  • 2
  • 36
  • 54