2015-06-09 22 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

相关问题