以下代码应生成 5 个相同的 groupbox 元素,每个元素填充一个表单、一个按钮和一个进度条:
private void DrawControls()
{
for(int i=0; i<5; i++)
{
// define controls
GroupBox gbxAttachmentName = new GroupBox();
gbxAttachmentName.Text = "Datei.jpg";
gbxAttachmentName.Font = new Font(gbxAttachmentName.Font, FontStyle.Bold);
TextBox tbxAttachmentLabel = new TextBox();
tbxAttachmentLabel.Text = "Bezeichnung";
tbxAttachmentLabel.Font = new Font(tbxAttachmentLabel.Font, FontStyle.Regular);
Button btnUploadAttachment = new Button();
btnUploadAttachment.Text = "übertragen";
btnUploadAttachment.Font = new Font(btnUploadAttachment.Font, FontStyle.Regular);
ProgressBar pbUploadProgress = new ProgressBar();
pbUploadProgress.Step = 1;
// position controls
gbxAttachmentName.Size = new Size(500, 75);
gbxAttachmentName.Location = new Point(10, 10 + (i * 85));
tbxAttachmentLabel.Size = new Size(375, 20);
tbxAttachmentLabel.Location = new Point(10, 20 + (i * 85));
btnUploadAttachment.Size = new Size(100, 22);
btnUploadAttachment.Location = new Point(390, 19 + (i * 85));
pbUploadProgress.Size = new Size(480, 20);
pbUploadProgress.Location = new Point(10, 45 + (i * 85));
// add controls to groupbox
gbxAttachmentName.Controls.Add(tbxAttachmentLabel);
gbxAttachmentName.Controls.Add(pbUploadProgress);
gbxAttachmentName.Controls.Add(btnUploadAttachment);
// add groupbox to form
flpMain.Controls.Add(gbxAttachmentName);
}
}
相反,我只正确填写了 1 个 Groupbox。其他4个是空的。
有没有人解决这个问题?
提前致谢