如何在foreach循环中添加面板
How to add panel inside foreach loop
我刚刚在学习 c# windows 表单应用程序。几天来,我一直在尝试使用 foreach 循环将一些来自 List 的数据添加到面板,但那里出现了一些错误。它只显示第一个数据。请大家告诉我我哪里错了。我想显示所有来自列表的数据并将其绑定到面板,以便面板根据数据循环。
private void loopPanel(List<Bug> list)
{
foreach (var bug in list)
{
int x = 56;
Panel panel = new Panel();
this.panelBugs.Controls.Add(panel);
Label lblProject = new Label();
Label lblClass = new Label();
Label lblMethod = new Label();
PictureBox pictureBox = new PictureBox();
//
//panel
//
panel.BackColor = System.Drawing.Color.DarkOliveGreen;
panel.Controls.Add(lblMethod);
panel.Controls.Add(lblClass);
panel.Controls.Add(lblProject);
panel.Controls.Add(pictureBox);
panel.Location = new System.Drawing.Point(10, ++x+66);
panel.Name = panel + bug.BugId.ToString();
panel.Size = new System.Drawing.Size(535, 100);
panel.TabIndex = 1;
//panel.Paint += new System.Windows.Forms.PaintEventHandler(panel_Paint);
panel.SuspendLayout();
//
//lblProject
//
lblProject.AutoSize = true;
lblProject.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblProject.Location = new System.Drawing.Point(3, 3);
lblProject.Name = lblProject + bug.BugId.ToString();
lblProject.Size = new System.Drawing.Size(50, 16);
lblProject.TabIndex = 1;
lblProject.Text = "Project: "+ bug.ProjectName;
//
//lblClass
//
lblClass.AutoSize = true;
lblClass.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblClass.Location = new System.Drawing.Point(3, 34);
lblClass.Name = lblClass + bug.BugId.ToString();
lblClass.Size = new System.Drawing.Size(42, 16);
lblClass.TabIndex = 2;
lblClass.Text = "Class: " + bug.ClassName;
//
//lblMethod
//
lblMethod.AutoSize = true;
lblMethod.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblMethod.Location = new System.Drawing.Point(4, 71);
lblMethod.Name = lblMethod + bug.BugId.ToString();
lblMethod.Size = new System.Drawing.Size(53, 16);
lblMethod.TabIndex = 3;
lblMethod.Text = "Method: " + bug.MethodName;
//
//pictureBox
//
pictureBox.Location = new System.Drawing.Point(391, 3);
pictureBox.Name = pictureBox + bug.BugId.ToString();
pictureBox.Size = new System.Drawing.Size(141, 94);
pictureBox.TabIndex = 0;
pictureBox.TabStop = false;
((System.ComponentModel.ISupportInitialize)(pictureBox)).BeginInit();
panel.ResumeLayout(false);
panel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(pictureBox)).EndInit();
}
}
try
{
BugDAO bugDao = new BugDAO();
List<Bug> list = bugDao.getAllBugs();
loopPanel(list);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
谢谢!希望有积极的结果。
你把面板的位置按照彼此的顺序。
int lastTop=0;
foreach(....){ var p=new panel();p.Top=lastTop;lastTop=p.Height;}
我的问题在循环外替换 x
后得到解决。
private void loopPanel(List<Bug> list)
{
int x = 56;
foreach (var bug in list)
{//56
//int x = 0;
Panel panel = new Panel();
this.panelBugs.Controls.Add(panel);
Label lblProject = new Label();
Label lblClass = new Label();
Label lblMethod = new Label();
PictureBox pictureBox = new PictureBox();
//
//panel
//
panel.BackColor = System.Drawing.Color.DarkOliveGreen;
panel.Controls.Add(lblMethod);
panel.Controls.Add(lblClass);
panel.Controls.Add(lblProject);
panel.Controls.Add(pictureBox);
panel.Location = new System.Drawing.Point(10, x);
x += 105;
panel.Name = panel + bug.BugId.ToString();
panel.Size = new System.Drawing.Size(535, 100);
panel.TabIndex = 1;
//panel.Paint += new System.Windows.Forms.PaintEventHandler(panel_Paint);
panel.SuspendLayout();
//
//lblProject
//
lblProject.AutoSize = true;
lblProject.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblProject.Location = new System.Drawing.Point(3, 3);
lblProject.Name = lblProject + bug.BugId.ToString();
lblProject.Size = new System.Drawing.Size(50, 16);
lblProject.TabIndex = 1;
lblProject.Text = "Project: "+ bug.ProjectName;
//
//lblClass
//
lblClass.AutoSize = true;
lblClass.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblClass.Location = new System.Drawing.Point(3, 34);
lblClass.Name = lblClass + bug.BugId.ToString();
lblClass.Size = new System.Drawing.Size(42, 16);
lblClass.TabIndex = 2;
lblClass.Text = "Class: " + bug.ClassName;
//
//lblMethod
//
lblMethod.AutoSize = true;
lblMethod.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblMethod.Location = new System.Drawing.Point(4, 71);
lblMethod.Name = lblMethod + bug.BugId.ToString();
lblMethod.Size = new System.Drawing.Size(53, 16);
lblMethod.TabIndex = 3;
lblMethod.Text = "Method: " + bug.MethodName;
//
//pictureBox
//
pictureBox.Location = new System.Drawing.Point(391, 3);
pictureBox.Name = pictureBox + bug.BugId.ToString();
pictureBox.Size = new System.Drawing.Size(141, 94);
pictureBox.TabIndex = 0;
pictureBox.TabStop = false;
((System.ComponentModel.ISupportInitialize)(pictureBox)).BeginInit();
panel.ResumeLayout(false);
panel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(pictureBox)).EndInit();
}
}
我刚刚在学习 c# windows 表单应用程序。几天来,我一直在尝试使用 foreach 循环将一些来自 List 的数据添加到面板,但那里出现了一些错误。它只显示第一个数据。请大家告诉我我哪里错了。我想显示所有来自列表的数据并将其绑定到面板,以便面板根据数据循环。
private void loopPanel(List<Bug> list)
{
foreach (var bug in list)
{
int x = 56;
Panel panel = new Panel();
this.panelBugs.Controls.Add(panel);
Label lblProject = new Label();
Label lblClass = new Label();
Label lblMethod = new Label();
PictureBox pictureBox = new PictureBox();
//
//panel
//
panel.BackColor = System.Drawing.Color.DarkOliveGreen;
panel.Controls.Add(lblMethod);
panel.Controls.Add(lblClass);
panel.Controls.Add(lblProject);
panel.Controls.Add(pictureBox);
panel.Location = new System.Drawing.Point(10, ++x+66);
panel.Name = panel + bug.BugId.ToString();
panel.Size = new System.Drawing.Size(535, 100);
panel.TabIndex = 1;
//panel.Paint += new System.Windows.Forms.PaintEventHandler(panel_Paint);
panel.SuspendLayout();
//
//lblProject
//
lblProject.AutoSize = true;
lblProject.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblProject.Location = new System.Drawing.Point(3, 3);
lblProject.Name = lblProject + bug.BugId.ToString();
lblProject.Size = new System.Drawing.Size(50, 16);
lblProject.TabIndex = 1;
lblProject.Text = "Project: "+ bug.ProjectName;
//
//lblClass
//
lblClass.AutoSize = true;
lblClass.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblClass.Location = new System.Drawing.Point(3, 34);
lblClass.Name = lblClass + bug.BugId.ToString();
lblClass.Size = new System.Drawing.Size(42, 16);
lblClass.TabIndex = 2;
lblClass.Text = "Class: " + bug.ClassName;
//
//lblMethod
//
lblMethod.AutoSize = true;
lblMethod.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblMethod.Location = new System.Drawing.Point(4, 71);
lblMethod.Name = lblMethod + bug.BugId.ToString();
lblMethod.Size = new System.Drawing.Size(53, 16);
lblMethod.TabIndex = 3;
lblMethod.Text = "Method: " + bug.MethodName;
//
//pictureBox
//
pictureBox.Location = new System.Drawing.Point(391, 3);
pictureBox.Name = pictureBox + bug.BugId.ToString();
pictureBox.Size = new System.Drawing.Size(141, 94);
pictureBox.TabIndex = 0;
pictureBox.TabStop = false;
((System.ComponentModel.ISupportInitialize)(pictureBox)).BeginInit();
panel.ResumeLayout(false);
panel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(pictureBox)).EndInit();
}
}
try
{
BugDAO bugDao = new BugDAO();
List<Bug> list = bugDao.getAllBugs();
loopPanel(list);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
谢谢!希望有积极的结果。
你把面板的位置按照彼此的顺序。
int lastTop=0;
foreach(....){ var p=new panel();p.Top=lastTop;lastTop=p.Height;}
我的问题在循环外替换 x
后得到解决。
private void loopPanel(List<Bug> list)
{
int x = 56;
foreach (var bug in list)
{//56
//int x = 0;
Panel panel = new Panel();
this.panelBugs.Controls.Add(panel);
Label lblProject = new Label();
Label lblClass = new Label();
Label lblMethod = new Label();
PictureBox pictureBox = new PictureBox();
//
//panel
//
panel.BackColor = System.Drawing.Color.DarkOliveGreen;
panel.Controls.Add(lblMethod);
panel.Controls.Add(lblClass);
panel.Controls.Add(lblProject);
panel.Controls.Add(pictureBox);
panel.Location = new System.Drawing.Point(10, x);
x += 105;
panel.Name = panel + bug.BugId.ToString();
panel.Size = new System.Drawing.Size(535, 100);
panel.TabIndex = 1;
//panel.Paint += new System.Windows.Forms.PaintEventHandler(panel_Paint);
panel.SuspendLayout();
//
//lblProject
//
lblProject.AutoSize = true;
lblProject.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblProject.Location = new System.Drawing.Point(3, 3);
lblProject.Name = lblProject + bug.BugId.ToString();
lblProject.Size = new System.Drawing.Size(50, 16);
lblProject.TabIndex = 1;
lblProject.Text = "Project: "+ bug.ProjectName;
//
//lblClass
//
lblClass.AutoSize = true;
lblClass.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblClass.Location = new System.Drawing.Point(3, 34);
lblClass.Name = lblClass + bug.BugId.ToString();
lblClass.Size = new System.Drawing.Size(42, 16);
lblClass.TabIndex = 2;
lblClass.Text = "Class: " + bug.ClassName;
//
//lblMethod
//
lblMethod.AutoSize = true;
lblMethod.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
lblMethod.Location = new System.Drawing.Point(4, 71);
lblMethod.Name = lblMethod + bug.BugId.ToString();
lblMethod.Size = new System.Drawing.Size(53, 16);
lblMethod.TabIndex = 3;
lblMethod.Text = "Method: " + bug.MethodName;
//
//pictureBox
//
pictureBox.Location = new System.Drawing.Point(391, 3);
pictureBox.Name = pictureBox + bug.BugId.ToString();
pictureBox.Size = new System.Drawing.Size(141, 94);
pictureBox.TabIndex = 0;
pictureBox.TabStop = false;
((System.ComponentModel.ISupportInitialize)(pictureBox)).BeginInit();
panel.ResumeLayout(false);
panel.PerformLayout();
((System.ComponentModel.ISupportInitialize)(pictureBox)).EndInit();
}
}