日期:2014-05-17  浏览次数:21137 次

c#如何获取子控件在父容器的位置
自定义了一个控件,要在这个自定义控件中写一些方法,在里面有个事件显示个panel,这个panel的位置要在自定义控件内的一个label位置。

但是我定义的

 int x = this.label1.Location.X;
 int y = this.label1.Location.Y + this.label1.Height;
 panel.Location = new Point(x, y);

结果显示到父窗体中,就直接是0,0,也就是这个label1在自定义控件中的位置坐标。

现在怎样,在这个自定义控件中,就能获取的到该自定义控件在父窗体的坐标呢,谢谢啦

在线等待... ...

------解决方案--------------------
实例化父窗体。
传到子窗体中。
------解决方案--------------------
没看明白.... - -
------解决方案--------------------
你在可以 自定义控件中,建一个方法,每一次打开窗体后执行一次。
如:
C# code

 public void UpdatePanelLocation()
        {
            int x = this.label1.Location.X;
            int y = this.label1.Location.Y;
            this.panel1.Location = new Point(x, y);
        }

然后在:
  private void Form1_Load(object sender, EventArgs e)
        {
            this.userControl11.UpdatePanelLocation();
        }