当前位置: 代码迷 >> .NET Framework >> c# panel 布局有关问题
  详细解决方案

c# panel 布局有关问题

热度:206   发布时间:2016-05-01 23:32:01.0
c# panel 布局问题
在后台代码动态的向一个已经存在的Panel: pnlParent  的中添加两个Panel  :  pnl_top, pnl_fill

最后pnlfill填充了整个 pnlParent;

            pnlParent.Height = 300;

            Panel pnl_top = new Panel();
            pnl_top.Size = new Size(100,150);
            pnl_top.BackColor = Color.Blue;
            this.pnlParent.Controls.Add(pnl_top);
            pnl_top.Dock = DockStyle.Top;


            Panel pnl_fill = new Panel();
            pnl_fill.BackColor = Color.Red;
            this.pnlParent.Controls.Add(pnl_fill);
            pnl_fill.Dock = DockStyle.Fill;

            MessageBox.Show("pnlParent.Height : " + pnlParent.Height.ToString() + "     pnl_fill.Height:"+pnl_fill.Height.ToString());


------解决方案--------------------

            pnlParent.Height = 300;

            Panel pnl_top = new Panel();
            pnl_top.BackColor = Color.Blue;
            this.pnlParent.Controls.Add(pnl_top);
            pnl_top.Dock = DockStyle.Top;
            pnl_top.SendToBack();

            Panel pnl_fill = new Panel();
            pnl_fill.BackColor = Color.Red;
            this.pnlParent.Controls.Add(pnl_fill);
            pnl_fill.Dock = DockStyle.Fill;
            pnl_fill.BringToFront();

------解决方案--------------------
参照3楼的再加你自己定义的大小可行

panel1.Height = 600;
            panel1.BackColor = Color.Aqua;
            Panel pnl_top = new Panel();
            pnl_top.BackColor = Color.Blue;
            pnl_top.Size = new Size(100, 150);/////////////
            this.panel1.Controls.Add(pnl_top);
            pnl_top.Dock = DockStyle.Top;
            pnl_top.SendToBack();

            Panel pnl_fill = new Panel();
            pnl_fill.BackColor = Color.Red;
            this.panel1.Controls.Add(pnl_fill);
            pnl_fill.Dock = DockStyle.Fill;
            pnl_fill.BringToFront();

            MessageBox.Show("pnlParent.Height : " + panel1.Height.ToString() + "     pnl_fill.Height:" + pnl_fill.Height.ToString());

------解决方案--------------------

            pnlParent.Height = 300;

            Panel pnl_top = new Panel();
            pnl_top.Size = new Size(100,150);
            pnl_top.BackColor = Color.Blue;
            this.pnlParent.Controls.Add(pnl_top);
            pnl_top.Dock = DockStyle.Top;


            Panel pnl_fill = new Panel();
            pnl_fill.BackColor = Color.Red;
            this.pnlParent.Controls.Add(pnl_fill);