2013-04-23 60 views
2

我已經設置了一些拆分容器以及一個關於文本框和標籤高度的面板。我希望標籤留在文本框的左側,文本框的寬度基本上會到面板的邊緣(如向右延伸)。如何添加標籤和文本框以填充水平面板?

有沒有一種簡單的方法來使用flowlayoutpanel或tablepanel或其他方法來做到這一點。我以編程方式添加控件(不使用窗體編輯器)。

理想情況下,如果面板增長,文本框應該拉伸。

+1

在C#中查找'Dock','Anchor'和'Size'屬性。我認爲這是Winforms? – Brian 2013-04-23 15:25:28

+0

您應該能夠將這些組件控件直接包含到* Panel *中。然後將* Dock或Anchor *屬性分配給位置。所以,是的...你可以。 – Greg 2013-04-23 15:25:43

+0

我正在嘗試使用flowlayoutpanel。 Dock和Anchor似乎很難工作。這是Winforms。 – Derek 2013-04-23 15:28:07

回答

0

你可能想要做的是根據面板尺寸計算標籤和文本框的寬度和高度。

對於職位,你可能必須給他們一個硬編碼的起始位置,但是,這又可以基於某些計算。

如果將它們放置在tablelayout面板中的一個面板中,那麼它們應該在表單/容器控制器增長時自動調整它們自己的大小,但要確保可以使用anchor屬性來確保這一點。

例如,將面板停靠在tablelayout面板單元內的填充模式,然後假定左側的標籤和右側的文本框將左側的標籤和右側的文本框固定。這應確保控制器的這些邊緣粘在面板上。

0

當您偏離窗體設計器視圖您的代碼將稍微更具體。當你使用設計器時,你可以通過拖放實現這樣的事情。但爲了做這樣的事情在你的代碼會做一些如:

private void InitializeComponent() 
    { 
     this.panel1 = new System.Windows.Forms.Panel(); 
     this.label1 = new System.Windows.Forms.Label(); 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.panel1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // panel1 
     // 
     this.panel1.Controls.Add(this.textBox1); 
     this.panel1.Controls.Add(this.label1); 
     this.panel1.Location = new System.Drawing.Point(12, 12); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(400, 358); 
     this.panel1.TabIndex = 0; 
     this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); 
     // 
     // label1 
     // 
     this.label1.AutoSize = true; 
     this.label1.Dock = System.Windows.Forms.DockStyle.Left; 
     this.label1.Location = new System.Drawing.Point(0, 0); 
     this.label1.Name = "label1"; 
     this.label1.Size = new System.Drawing.Size(35, 13); 
     this.label1.TabIndex = 0; 
     this.label1.Text = "label1"; 
     // 
     // textBox1 
     // 
     this.textBox1.Dock = System.Windows.Forms.DockStyle.Right; 
     this.textBox1.Location = new System.Drawing.Point(47, 0); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(353, 20); 
     this.textBox1.TabIndex = 1; 
     this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(424, 382); 
     this.Controls.Add(this.panel1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.Load += new System.EventHandler(this.Form1_Load); 
     this.panel1.ResumeLayout(false); 
     this.panel1.PerformLayout(); 
     this.ResumeLayout(false); 

    } 

正如你可以在運行時組件被初始化看;那麼所有的屬性都被賦予適當的定位。您的標記相對於表格面板的佈局。通過定義點可以確保它們集中。

這應該讓你開始;但絕不會是理想的。您可能需要配置不同的項目以確保其符合您的標準。但是在440×420像素處具有形式。小組還停靠在整個佈局的半英寸範圍內。您的文本框和標籤固定在屏幕的左上角和右側部分。

請記住,如果您最大化此佈局,它可能會以不利的方式調整設計,除非它們被鎖定到這些特定位置。希望有幫助。

+0

我得到了與此相同的答案。 – 2013-04-24 07:16:01