c# - 如何在foreach循环中添加面板
问题描述
我只是在学习 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);
}
谢谢!希望有积极的结果。
解决方案
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# - 控制器的 ASP.NET Core 路由和索引操作
- rust - 如何忽略从 Rust 函数返回的错误并继续进行?
- akka - 使用 akka 流或 akka 演员进行动态管道创建
- python - 使用 Python 的 Azure 计时器触发功能
- reactjs - 使用 Pandoc 将 React 网页转换为 pdf
- python - 设置全局变量 Kivy
- c++ - Qt中的Pimpl成语用法,寻找简洁的方式
- python - 子模块中忽略的 Python 日志记录处理程序
- python - 不同行之间的计算
- actions-on-google - 删除 Alpha 发布的 Google Home 操作的调用要求