我在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"这样的方式设置他们的属性?
我现在的做法是一个个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"这样的方式设置他们的属性?
解决方案 »
- 图片读入流后想转为字符串
- 关于C#读取VB6程序保存的文件
- datagrid 相同資料行合併問題
- 如何在水晶报表中动态显示通过GDI+绘制的图形对象?急急急...不够再加分
- 急......求类似QQ窗体的源码或者构件
- ArrayList在vc6中应该是什么类型,该怎么转换?
- 关于用户控件中调用方法
- C#一个方法传入一个算术参数,如何计算这个算术题并返回一个Int值
- 关于数组的问题,以下结果为什么lbt[59] ,lbt[60]都能够访问,不报异常 ?
- 求所有的using指令集
- WINFORM中我确定按钮绑定了窗体的AcceptButton,本来我有个TextBox有回车事件,但他不会执行,直接执行确定按钮,我应该怎么做,急
- 设置DataGridView里的ValueType的方法
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);
}
lbl.Name="Label" + i.ToString();
lbl.Text="Label" + i.ToString();
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"
}
但是可能不用用设计器了
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;
}
}
Controls没Add lbl (this.Controls.Add(lbl);)
如何找得到!
{
foreach(Control c in Controls)
{
if(c.GetType().Equals(type) && c.Name.Equals(name)) //窗体控件中有Type和当前请求的控件一样
{
return f;
}
}
} MessageBox.Show(GetControlInstance(typeof(Label),"label10"));
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();
}