[英]Adding objects during run time?
I have a tab which is made active if there is more than one record returned from a query on my database. 我有一个选项卡,如果我的数据库中的查询返回了多个记录,该选项卡将变为活动状态。
For each record returned I would like a set of labels created and placed on the tab. 对于返回的每条记录,我希望创建一组标签并将其放置在选项卡上。 For example if there are 8 records I would like 8 labels created.
例如,如果有8条记录,我希望创建8个标签。
My loop only creates one label, even though my count is showing I have 8 records? 即使计数显示我有8条记录,我的循环也只会创建一个标签? Not sure why?
不知道为什么吗?
How do you create labels in a loop 8 times and not have them draw in the same location 8 times? 如何在循环中创建标签8次而又不在同一位置绘制8次? I would them to appear in a horizontal list.
我希望他们出现在水平列表中。 Pretty sure the way I have coded the solution ,they will all be drawn in the same place?
我很确定我编写解决方案的方式,它们都将在同一位置绘制吗?
for (int i = 1; i <= rowCount; i++)
{
// Create objects
LinkLabel Linklabel1 = new LinkLabel();
Linklabel1.Text += ds.Tables[0].Rows[0]["code"].ToString();
Linklabel1.Location = new Point(10, 50);
Linklabel1.Height = 40;
Linklabel1.Width = 100;
tabControl1.TabPages[0].Controls.Add(Linklabel1);
}
Try something like this out: 尝试这样的事情:
for (int i = 0; i < rowCount; i++)
{
// Create objects
LinkLabel Linklabel1 = new LinkLabel();
Linklabel1.Text = ds.Tables[0].Rows[i]["code"].ToString();
Linklabel1.Height = 40;
Linklabel1.Width = 100;
Linklabel1.Location = new Point((i + 1) * 10 + (i * Linklabel1.Width), 50);
tabControl1.TabPages[0].Controls.Add(Linklabel1);
}
If you don't want to explicitly position them by setting the Location() property, consider putting a FlowLayoutPanel on the TabPage and added the controls to that instead. 如果您不想通过设置Location()属性来显式定位它们,请考虑将FlowLayoutPanel放在TabPage上,然后向其中添加控件。 Then they will positioned automatically for you.
然后他们会自动为您定位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.