自定义了一个控件,要在这个自定义控件中写一些方法,在里面有个事件显示个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在自定义控件中的位置坐标。现在怎样,在这个自定义控件中,就能获取的到该自定义控件在父窗体的坐标呢,谢谢啦在线等待... ...

解决方案 »

  1.   


    这貌似不好吧,不符合mvc啊
      

  2.   

    你在可以 自定义控件中,建一个方法,每一次打开窗体后执行一次。
    如: 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();
            }
      

  3.   

    解决了,这样用是没问题的,只是我想用用this.FindForm()取代this.Parent,this.FindForm().Controls.Add(panel);加的就不对
    换this.Parent.Controls.Add(panel)就行了。
    本来我还想换个新的属性用用,结果就成这了。
      

  4.   

    http://blog.csdn.net/yysyangyangyangshan
      

  5.   

    看你的需求要干什么,最好的是通过事件来传递
    自定义空间中
     public partial class TaskConnten : UserControl
        {
            public delegate void EventDelegate(object sender, EventArgs e);        public event EventDelegate DeleConnten; 
            public TaskConnten()
            {}
     private void Coles_Paint(object sender, EventArgs e)
            {
                MessageBox.Show("你确定要删除该任务吗");
                try
                {
                    if (DeleConnten != null) 
                    {
                        DeleConnten(this, new EventArgs()); 
                    }
                }
                catch(Exception x)
                { 
                   
                }
            }
        }窗体中
    生窗体
     public void ShowData()
            {
               TaskConnten tc = new TaskConnten(T,inter);
               tc.DeleConnten += new TaskConnten.EventDelegate(DeleConnten);           this.panel1.Controls.Add(tc);
               tc.Top = (this.panel1.Controls.count-1) * tc.Size.Height;
               tc.Show();
            }
    触发事件后执行的方法
    public void DeleConnten(object sender,EventArgs e)
            {
                TaskConnten deleObjer = sender as TaskConnten;
                this.panel1.Controls.Clear();
                for (int i = 0; i < tcs.Count;i++ )
                {
                    if (tcs[i] == deleObjer || tcs[i].IsDisposed)
                    {
                        tcs.Remove(tcs[i]);
                        deleObjer.Dispose();
                        //i = i - 1;
                    }
                    if (tcs.Count > 0)
                    {
                        this.panel1.Controls.Add(tcs[i]);
                        tcs[i].Top = i * tcs[i].Size.Height;
                        tcs[i].Show();
                    }
                }
            }