繁体   English   中英

在flowlayoutpanel内部的用户控件中找到按钮控件的位置

[英]find the position of button control inside the user control inside flowlayoutpanel

我在windows窗体的flowlayoutpanel中有20个用户控件。

每个用户控件都有一个按钮。

我想在flowlayoutpanel上找到每个按钮的位置。 如何找到按钮的X和Y坐标?

我可以像这样访问按钮:

foreach (Control ctrl in this.pnlContainer.Controls.Find("btnPrint",true))
{
    Button c = ctrl as Button;
    if (c != null)
    {
        logger.Info("x: "+c.Location.X + ",y: "+c.Location.Y,c);
    }
}

但是,x和y坐标始终相同。

谢谢!

如果按钮在所有UC中都位于同一位置,则XY中所有按钮的xy都将相同,并且您获得的位置是相对于UC而不是窗体。

我想你可以在这里找到它

C#获取控件在窗体上的位置

试试这个代码

foreach (Control ctrl in this.flowLayoutPanel1.Controls)
            {
                foreach (Control item in ctrl.Controls.Find("button1", true))
                {
                    Point pointOnForm = new Point(0, 0);
                    Control Btn = item;
                    for (; Btn.Parent != null && Btn.Parent.GetType() != typeof(Form); Btn = Btn.Parent)
                    {
                        pointOnForm.Offset(Btn.Location);
                    }

                    //label2.Text += pointOnForm + ",";
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM