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 :(得分:0)
如Peter Duniho所示,您可以使用两个TableLayoutPanel,一个在设计时添加到您的表单。另一个是动态创建的,并使用正确的控件进行填充,以实现您的需求。你的代码看起来像这样:
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++;
}
您将使用Dock和Anchor属性以及AutoSize属性让框架的布局引擎为您完成所有繁重工作。
你最终会看到这样的东西: