2012-05-08 31 views
5

我有一個內部有兩個控件的面​​板。我希望它們粘在面板的邊框上(面板有一些寬度和高度不能改變),但有可能調整它們(控制)從面板垂直方向獲得的空間量。c#win窗體使控件可調整大小

panel.Controls.Add(listview1); 
panel.Controls.Add(listview2); 

兩個listview被一個接一個放置(垂直)。我希望有可能「改變它們的高度」(通過選擇它們之間的邊界來調整大小)。

我希望你明白我的意思。提前感謝您的幫助。

+2

基本上你希望能夠改變每個列表視圖在其父容器中佔用的區域,但讓它們共享一個邊框,以便一邊增長另一邊縮小以佔據剩餘空間? (只是爲了讓其他用戶更清楚) – RhysW

+0

您可以查看(例如)WinForms中的TableLayoutPanel控件。您可以使用它來創建某種網格。鏈接:http://msdn.microsoft.com/en-us/library/h21wykkx.aspx和http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx – Styxxy

+0

你想讓用戶通過拖動邊緣來手動更改控件的大小? – nawfal

回答

1

你有沒有在列表視圖使用錨考慮?

 this.panel1 = new System.Windows.Forms.Panel(); 
     this.listView1 = new System.Windows.Forms.ListView(); 
     this.listView2 = new System.Windows.Forms.ListView(); 
     this.panel1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // panel1 
     // 
     this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     | System.Windows.Forms.AnchorStyles.Left) 
     | System.Windows.Forms.AnchorStyles.Right))); 
     this.panel1.Controls.Add(this.listView2); 
     this.panel1.Controls.Add(this.listView1); 
     this.panel1.Location = new System.Drawing.Point(12, 12); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(413, 280); 
     this.panel1.TabIndex = 0; 
     // 
     // listView1 
     // 
     this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
     | System.Windows.Forms.AnchorStyles.Right))); 
     this.listView1.Location = new System.Drawing.Point(3, 0); 
     this.listView1.Name = "listView1"; 
     this.listView1.Size = new System.Drawing.Size(410, 97); 
     this.listView1.TabIndex = 0; 
     this.listView1.UseCompatibleStateImageBehavior = false; 
     // 
     // listView2 
     // 
     this.listView2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
     | System.Windows.Forms.AnchorStyles.Right))); 
     this.listView2.Location = new System.Drawing.Point(0, 183); 
     this.listView2.Name = "listView2"; 
     this.listView2.Size = new System.Drawing.Size(410, 97); 
     this.listView2.TabIndex = 1; 
     this.listView2.UseCompatibleStateImageBehavior = false; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(437, 304); 
     this.Controls.Add(this.panel1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.panel1.ResumeLayout(false); 
     this.ResumeLayout(false); 
2

將上方的doc屬性設置爲上方。 在同一個容器(面板)中添加一個方向爲垂直的分隔欄。設置較低的Dock屬性以填充。 無論如何都是這樣做的一種方法。

2

我同意保羅的說法,SplitContainer就是你要找的。我會補充說,你需要設置你放在分離容器內的控件的Dock和Anchor屬性。如果您將子控件的Dock屬性設置爲Fill,它將展開以填充整個容器,而不管面板的大小如何。如果面板中有多個控件,則使用Anchor屬性。在這種情況下,您可以設置子控件的Anchor屬性,告訴孩子控制哪些面「粘」到容器的一側。請參閱this page以更全面地瞭解這兩個屬性。

此外,您還需要設置或Anchor屬性的SplitContainer控件本身。這會在表單調整大小時調整大小。然後在SplitContainer內的子控件上設置Anchor/Dock屬性將導致子控件在容器調整大小時調整大小。