Win Form 的 Dock和Splitter
如果在Form上放一个 Panel ,panel.Dock = Left
在放一个Splitter,Splitter的Dock缺省为Left, 不能为None,也不能为Fill.
注意此时:
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 0);
…
this.splitter1.Location = new System.Drawing.Point(200, 0);
…
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
这时的拖动Splitter, Panel会随之变化.
如果把
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
的顺序换一下,打开Designer就会看到,splitter就会被放在form的最左边,
运行时splitter也会被放在form的最左边.
此时代码尚无任何改变,把form的size改一下,导致designer产生代码,就会看到:
this.splitter1.Location = new System.Drawing.Point(0, 0);
this.panel1.Location = new System.Drawing.Point(3, 0);
!–可见对于使用了dock的control,他们的location实际是由deisnger,或在
运行时算出来的,指定的值并无效果.
对于指定了相同dock的多个control,比如panel1和splitter,都要dock到Left,最左边的
Control必须最后加到form.Controls中
大多情况下,form上splitter的右边还会有一个dock属性为fill的panel,
正常的顺序是先添加panel_left,再添加splitter,再添加panel_right,
此时生成的代码顺序是:
this.panel_Right.Location = new System.Drawing.Point(272, 0); //Note
…
this.Controls.Add(this.panel_Right);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel_Left);
如果把代码调整为:
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel_Left);
this.Controls.Add(this.panel_Right);
panel_right就会fill到整个form,而不是splitter右边的区域:
this.panel_Right.Location = new System.Drawing.Point(0, 0); //Note
this.panel_Right.Size = new System.Drawing.Size(640, 533);
!–可见,fill的control要最先加到容器中.
查看这个问题有个好办法,把一个button放到panel_right的左上角,如果button的location不接近(0,0),
就说明有问题.
再进一步,在Panel_Right上加三个panel:
Top(Dock=Top), Center(Dock=Fill), Bottom(Dock=Bottom)
添加的顺序为Top, Botton, Center,
生成的代码顺序为:
this.panel_Right.Controls.Add(this.panel_Center);
this.panel_Right.Controls.Add(this.panel_Bottom);
this.panel_Right.Controls.Add(this.panel_Top);
现在,我已经知道这个顺序的奥妙了,我不会再尝试改变这个顺序.
