我努力在foreach循环中创建控件名称。
我需要创建 1 Button
, 1 Label
和 1 Panel
。< / p>
i = 0;
foreach (DataRow eachrow in Content.Rows)
{
this.tableLayoutPanel_content.RowCount = this.tableLayoutPanel_content.RowCount + 1;
this.tableLayoutPanel_content.RowStyles.Add(
new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
this.tableLayoutPanel_content.Controls.Add(this.panel_content_1, 0, i);
//
// panel_content
//
this.panel_content_[i].Controls.Add(this.button_[i];
this.panel_content_[i].Controls.Add(this.label_[i]);
this.panel_content_[i].Dock = System.Windows.Forms.DockStyle.Top;
this.panel_content_[i].Location = new System.Drawing.Point(0, 203);
this.panel_content_[i].Margin = new System.Windows.Forms.Padding(0);
this.panel_content_[i].Name = "panel_content_" + i;
this.panel_content_[i].Size = new System.Drawing.Size(960, 30);
this.panel_content_[i].TabIndex = 0;
...
i++;
}
我如何解决为每个foreach循环创建单独的Contol名称的问题?
答案 0 :(得分:0)
如果您要为Panel
用Label
和Button
创建eachrow
,可以尝试明确:
i = 0;
foreach (DataRow eachrow in Content.Rows) {
// Panel:
var myPanel = new Panel() {
Parent = this, // instead of this.Controls.Add()
Name = string.Format("panel_content_{0}", i),
Dock = System.Windows.Forms.DockStyle.Top,
//TODO: Put other properties here
};
// Label on myPanel
var myLabel = new Label() {
Parent = myPanel,
Name = string.Format("label_content_{0}", i),
//TODO: Put other properties here
};
// Button on myPanel
var myButton = new Button() {
Parent = myPanel,
Name = string.Format("button_content_{0}", i),
//TODO: Put other properties here
};
i += 1;
}