c# winform批量生成控件

在flowLayoutPanel垂直放置三十个Lable控件,并可以引用。

public partial class Form1 : Form
{
    private System.Windows.Forms.Label[] riskLable;

    private void Form1_Load(object sender, EventArgs e)
    {
        this.riskLable  = new System.Windows.Forms.Label[30];

        for (int i = 0; i < 30; i++)
        {
            this.riskLable[i] = new System.Windows.Forms.Label();
            this.riskLable[i].BackColor = Color.Green;
            this.riskLable[i].AutoSize = false;
            this.riskLable[i].Size = new System.Drawing.Size(flowLayoutPanel1.Size.Width, flowLayoutPanel1.Size.Height / 30);
            this.riskLable[i].Name = "riskLable" + i.ToString();
            this.flowLayoutPanel1.Controls.Add(this.riskLable[i]);
            }


        this.riskLable[10].BackColor = Color.Yellow;
    }

}

效果展示(红色部分):

ref:

https://bbs.csdn.net/topics/210068808

你可能感兴趣的:(C#)