我在一个Panel控件中动态添加几个Label控件,发现只能生成第一个,其他的都看不见。
代码如下:
                for (int i = 0; i < RowCount; i++)
                {
                    Label LB = new Label();
                    LB.Name = "LabelNew" + i.ToString();
                    LB.Font = font;
                    LB.BackColor = Color.Transparent;
                    LB.Text = "xxxxxx";
                    LB.AutoSize = true;
                    panel1.Controls.Add(LB);
                    LB.Location = new Point(9 + i * 5, 17);
                }
可能是因为都放在一起了,有什麽好办法让其他的也都能显示呢?这个问题困扰我很久了。

解决方案 »

  1.   

     for (int i = 0; i < RowCount; i++) 
                    { 
                        Label LB = new Label(); 
                        LB.ID = "LabelNew" + i.ToString(); 
                        LB.Name = "LabelNew" + i.ToString(); 
                        LB.Font = font; 
                        LB.BackColor = Color.Transparent; 
                        LB.Text = "xxxxxx"; 
                        LB.AutoSize = true; 
                        panel1.Controls.Add(LB); 
                        LB.Location = new Point(9 + i * 5, 17); 
                    } 
      

  2.   

    这里代码好像没什么问题,是不是你的ROWCOUNT只等于1?
    监视一下..看panel1.Controls.Count是多少
      

  3.   

     改为LB.Location = new Point(9 + i * 5, 17*i); 试一试
      

  4.   

    是你的坐标算不对。参考一下:for (int i = 0; i < 20; i++)
    {
    Label LB = new Label();
    LB.Name = "LabelNew" + i.ToString();
    LB.Font = this.Font;
    LB.BackColor = Color.Transparent;
    LB.Text = "xxxxxx";
    LB.AutoSize = true;
    this.Controls.Add(LB);
    LB.Location = new Point(9 + i * LB.Width, 17);
    }
      

  5.   

     LB.Location = new Point(9 + i * 5, 17); 
    的原因。
     LB.Location = new Point(9, 17+i * 5);
    最好设置他的height跟top