C# 标签数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/962449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array of Labels
提问by dpetek
How to create array of labels with Microsoft Visual C# Express Edition ? Is there way to do it with graphical (drag'n'drop) editor or I have to manually add it to auto generated code ?
如何使用 Microsoft Visual C# Express Edition 创建标签数组?有没有办法用图形(拖放)编辑器来做,或者我必须手动将它添加到自动生成的代码中?
采纳答案by nightcoder
You have to manually add it. But don't add it to auto generated code as it can be overwritten by Visual Studio designer.
您必须手动添加它。但是不要将它添加到自动生成的代码中,因为它可能会被 Visual Studio 设计器覆盖。
I would add it in Load event handler for the form. The code can look like this:
我会将它添加到表单的 Load 事件处理程序中。代码可能如下所示:
Label[] labels = new Label[10];
labels[0] = new Label();
labels[0].Text = "blablabla";
labels[0].Location = new System.Drawing.Point(100, 100);
...
labels[9] = new Label();
...
PS. Your task seems a little unusual to me. What do you want to do? Maybe there are better ways to accomplish your task.
附注。你的任务在我看来有点不寻常。你想让我做什么?也许有更好的方法来完成你的任务。
回答by C. Ross
You can add the labels to the form using the GUI editor, then add those to the array in form load.
您可以使用 GUI 编辑器将标签添加到表单中,然后在表单加载中将这些标签添加到数组中。
Label[] _Labels = new Label[3];
private void MyForm_Load(object sender, EventArgs e)
{
_Labels[0] = this.Label1;
_Labels[1] = this.Label2;
_Labels[2] = this.Label3;
}
This will at least make setting the location easier. Also you might want to consider using the FlowLayoutPanelif you're dynamically creating labels (or any control really).
这至少会使设置位置更容易。如果您正在动态创建标签(或任何控件),您也可能需要考虑使用FlowLayoutPanel。
回答by Ali Irawan
Label[ , ] _arr = new Label[4 , 4];
private void Form1_Load(object sender, EventArgs e)
{
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
_arr[i ,j] = new Label();
_arr[i ,j].Text = ""+i+","+j;
_arr[i ,j].Size = new Size(50,50);
_arr[i ,j].Location = new Point(j*50,i*50);
//you can set other property here like Border or else
this.Controls.Add(_arr[i ,j]);
}
}
}
if you want to set Border of Label in C# maybe you should check http://msdn.microsoft.com/en-us/library/system.windows.forms.label.aspx
如果你想在 C# 中设置标签边框也许你应该检查 http://msdn.microsoft.com/en-us/library/system.windows.forms.label.aspx
Label have property called Border. Please check it. Thanks
标签具有称为边框的属性。请检查一下。谢谢
回答by Jose Quispe
int i=0;
ControlNum=10;
Label[] lblExample= new Label[];
for(i=0;i<ControlNum;i++)
{
lblExample[i] = new Label();
lblExample[i].ID="lblName"+i; //lblName0,lblName1,lblName2....
Form1.Controls.Add(lblExample[i]);
}
xD ...
xD ...
Joshit0..
Joshit0..