我在C# WinForm中使用了二十多个Label,并要每个Label进行Location设置。
我现在的做法是一个个Label声明并修改属性:
        private void Form1_Load(object sender, EventArgs e)
        {
            this.SuspendLayout();            Label label1 = new Label();
            label1.Size = new Size(30, 30);
            label1.Image = Image.FromFile(@"F:\作品\c#作品\推箱子游戏\PushBox\PushBox\wall.jpg");
            this.Controls.Add(label1);
//.........这里全是一样的代码,只不过Label的名字不一样而已
            Label label30 = new Label();
            label30.Size = new Size(30, 30);
            label30.Image = Image.FromFile(@"F:\作品\c#作品\推箱子游戏\PushBox\PushBox\wall.jpg");
            this.Controls.Add(label30);            this.ResumeLayout();
        }
上面的代码用于生成30个Label,之后我还要对每个Label设置Location,如:label5.Location = new Point(345, 555);
label9.Location = new Point(45, 86);
//这是无规律的我现在想请教一下,Form1_Load这个方法中生成的30个Label,能否通过循环生成,并将他们命名为lable1,label2,...label30,减少代码量,并且在下面能够通过label5.Text="srdr"这样的方式设置他们的属性?

解决方案 »

  1.   


    Label lbl;
    for(int i=0;i<30;i++)
    {
      lbl=new Label();
      lbl.Size = new Size(30, 30);
      lbl.Image = Image.FromFile(@"F:\作品\c#作品\推箱子游戏\PushBox\PushBox\wall.jpg");
      this.Controls.Add(lbl);
    }
      

  2.   

    new后加两句
    lbl.Name="Label" + i.ToString();
    lbl.Text="Label" + i.ToString();
      

  3.   

    经测试
            private void Form1_Load(object sender, EventArgs e)
            {
                for (int i = 1; i < 30; i++)
                {
                    Label lbl=new Label();
                    if(i==10)
                    {
                        lbl.Text="这是第10个";
                    }
                    lbl.Name="label"+i.ToString();
                }
                MessageBox.Show(lb110.Text);//这里发生错误,当前上下问不存在名称"lbl10"
            }
      

  4.   

    可以用控件数组
    但是可能不用用设计器了
    namespace CONTROLARRAY
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label[10];
                this.SuspendLayout();
                // 
                // label1
                // 
                for (int i = 0; i < 10;i++ )
                {
                    this.label1[i] = new System.Windows.Forms.Label();
                    this.label1[i].AutoSize = true;
                    this.label1[i].Location = new System.Drawing.Point(71, 54+i*20);
                    this.label1[i].Name = "label1";
                    this.label1[i].Size = new System.Drawing.Size(41, 12);
                    this.label1[i].TabIndex = 0;
                    this.label1[i].Text = "label1";
                }
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(292, 273);
                for(int i=0;i<10;i++)
                this.Controls.Add(this.label1[i]);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();        }        #endregion        private System.Windows.Forms.Label[] label1;
        }
    }
      

  5.   


    Controls没Add lbl   (this.Controls.Add(lbl);)
    如何找得到!
      

  6.   

    因为Name是动态生成的,所以可以先遍历找出相对应的引用,然后在调用private Control GetControlInstance(Type type,string name)
    {
    foreach(Control   c   in   Controls)   
    {   
    if(c.GetType().Equals(type) &&  c.Name.Equals(name)) //窗体控件中有Type和当前请求的控件一样
    {   
    return f; 
    }   
    }
    }  MessageBox.Show(GetControlInstance(typeof(Label),"label10"));
      

  7.   

    int[] a = new int[] { 1, 2, 3, 4, 5, 6 ,7,8};
                for (int i = 0; i < a.Length; i++)
                {                object o;
                    Button btn;
                    o = this.GetType().GetField("button" + (i+1).ToString(), System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(this);
                    if (o != null)
                    {
                        btn = (System.Windows.Forms.Button)o;
                        btn.Text = a[i].ToString();
                    }