2015-06-09 59 views
1

有沒有在C#東西像在CSS - "float: left | right | none | inherit"
我想顯示在我的表格 - TextBox及以下各TextBox - PictureBox。所以,我不知道每個TextBox的大小,也可以是不同的,在我的結果覆蓋PictureBoxes .... TextBoxes如何在表單中放置動態項目?

static int k = 1; 

for (int att = 0; att < feed.response.items[i].attachments.Count(); att++) 
{ 
    string switchCase = feed.response.items[i].attachments[att].type; 
    switch (switchCase) 
    { 
     case "photo":           
      TextBox text = new TextBox(); 
      text.Text =Encoding.UTF8.GetString(Encoding.Default.GetBytes(feed.response.items[i].text)); 
      Point p = new Point(20, 30 * k); 
      text.Location = p; 
      this.Controls.Add(text); 

      PictureBox mypic = new PictureBox(); 
      mypic.ImageLocation = feed.response.items[i].attachments[att].photo.photo_130; 
      mypic.Size = new Size(100, 100); 
      Point g = new Point(40, 160 * k); 
      mypic.Location = g; 
      this.Controls.Add(mypic); 
      k++; 
      break; 
.... 
    } 
} 
+0

嘗試使用谷歌中搜索如何將控件添加動態的形式的WinForms C# – MethodMan

+0

我會用一個網格佈局對於這種的東西 – Gino

+2

你可能希望['TableLayoutPanel'](https://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel(v = vs.110).aspx)與'ColumnCount'屬性設置爲「1」。如果沒有[可靠,_minimal_,_complete_代碼示例](http://stackoverflow.com/help/mcve)可以可靠地說明您的場景,那麼很難肯定地說,不必介意以代碼示例提供有用的答案。 –

回答

0

Peter Duniho表明您可以使用兩個TableLayoutPanels,一個在設計 - 添加時間到你的表格。另一個是動態創建和填充正確的控件來實現你想要的。您的代碼應該是這樣的:

private void AddOne() 
{ 
    // create a new tablelayoutpanel 
    // with 2 rows, 1 column 
    var table = new TableLayoutPanel(); 
    table.RowCount = 2; 
    table.RowStyles.Add(new RowStyle()); 
    table.RowStyles.Add(new RowStyle()); 
    table.ColumnCount = 1; 
    table.Dock = DockStyle.Fill; // take all space 

    // create a label, notice the Anchor setting 
    var lbl = new Label(); 
    lbl.Text = "lorem ipsum\r\nnext line\r\nthree"; 
    lbl.Anchor = AnchorStyles.Left | AnchorStyles.Right; 
    lbl.AutoSize = true; 
    // add to the first row 
    table.Controls.Add(lbl, 0, 0); 

    // create the picturebox 
    var pict = new PictureBox(); 
    pict.Image = Bitmap.FromFile(@"demo.png"); 
    pict.Anchor = AnchorStyles.Left | AnchorStyles.Right; 
    pict.SizeMode = PictureBoxSizeMode.Zoom; 
    // add to the second row 
    table.Controls.Add(pict, 0, 1); 

    //add the table to the main table that is on the form 
    mainTable.Controls.Add(table, 0, mainTable.RowCount); 
    // increase the rowcount... 
    mainTable.RowCount++; 

} 

你必須使用DockAnchor性能以及AutoSize屬性有框架的佈局引擎做所有繁重的你。

你最終的東西了,看起來像這樣:

table layout panel with two rows

相關問題