当单击子控件时调用FlowLayoutPanel Mousedown处理程序

本文关键字:FlowLayoutPanel Mousedown 处理 程序 调用 单击 控件 | 更新日期: 2023-09-27 18:13:20

我在VS 2010, Windows窗体控件工作。

我有一个扩展的FlowLayoutPanel,其中我动态添加按钮

我的问题是,MouseDownEventhandlerflowlayout planel只执行时,点击任何地方除了按钮。When clicked on button the MouseDownEventHandler for the FlowLayoutPanel is not called .

我尝试将函数连接到添加到Panel的按钮的Click事件处理程序。但我注意到延误,因此我在工作中遇到了问题。

有谁能帮我一下吗?

当单击子控件时调用FlowLayoutPanel Mousedown处理程序

这可能不是最好的方法,但它适用于我:

//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
    }
}

反射的方法:

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

你可以将全局事件绑定到流面板中的所有子控件,这对我来说很好。希望我有所帮助:)