首页 > 解决方案 > 如何在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);
}

谢谢!希望有积极的结果。

标签: c#winformspanel

解决方案


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();
        }
    }

推荐阅读