C# 如何在表单中动态放置项?
c语言中是否有类似于css的“float:left | right | none | inherit”?C# 如何在表单中动态放置项?,c#,winforms,C#,Winforms,c语言中是否有类似于css的“float:left | right | none | inherit”? 我想在我的表单中显示-TextBox和每个TextBox-PictureBox。所以我不知道每个TextBox的大小,它可能不同,结果我的pictureboxscoverTextBox static int k = 1; for (int att = 0; att < feed.response.items[i].attachments.Count(); att++) {
我想在我的表单中显示-
TextBox
和每个TextBox
-PictureBox
。所以我不知道每个TextBox
的大小,它可能不同,结果我的pictureboxs
coverTextBox
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;
....
}
}
static int k=1;
对于(int att=0;att
如所示,您可以使用两个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++;
}
您将使用和属性以及AutoSize属性让框架的布局引擎为您完成所有繁重的工作
您将得到如下结果:
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++;
}
如所示,您可以使用两个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++;
}
您将使用和属性以及AutoSize属性让框架的布局引擎为您完成所有繁重的工作
您将得到如下结果:
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++;
}
尝试使用google并搜索如何将控件动态添加到表单winforms C#我会使用gridlayout来处理这类东西,您可能需要将
ColumnCount
属性设置为1
。如果没有这一点,很难说清楚,不要介意用代码示例提供有用的答案。尝试使用google并搜索如何将控件动态添加到窗体winforms C#如果ColumnCount
属性设置为1
,我会使用gridlayout来处理这类内容。如果没有这一点,很难说清楚,不要介意用代码示例提供有用的答案。尝试使用google并搜索如何将控件动态添加到窗体winforms C#如果ColumnCount
属性设置为1
,我会使用gridlayout来处理这类内容。如果没有可靠的示例来说明您的场景,就很难确定,更不用说用代码示例提供有用的答案了。