C# 更简单的方法来创建图片框数组(48x 框)
C# Easier way to create array of pictureboxes (48x boxes)
项目环境:Visual Studio 2013,使用 Windows 表单模板。
这就是我目前拥有的,我假设有一种更简单的方法来创建这样的数组。我试过很多方法,但我对C#还不够了解。
PictureBox[] boxes = {
runeBox1, runeBox2, runeBox3, runeBox4, runeBox5, runeBox6, runeBox7, runeBox8,
runeBox9, runeBox10, runeBox11, runeBox12, runeBox13, runeBox14, runeBox15, runeBox16,
runeBox17, runeBox18, runeBox19, runeBox20, runeBox21, runeBox22, runeBox23, runeBox24,
runeBox25, runeBox26, runeBox27, runeBox28, runeBox29, runeBox30, runeBox31, runeBox32,
runeBox33, runeBox34, runeBox35, runeBox36, runeBox37, runeBox38, runeBox39, runeBox40,
runeBox41, runeBox42, runeBox43, runeBox44, runeBox45, runeBox46, runeBox47, runeBox48};
到目前为止我尝试过的是:
string[] strArr = new string[48];
for (int i = 1; i != 48; i++)
{
strArr[i] = "runebox" + i.ToString();
}
int count = 0;
foreach (object p in this.Controls)
{
if (p.GetType() == typeof(PictureBox))
{
if (((PictureBox)p).Name == strArr[count])
{
boxes[count] = ((PictureBox)p);
count++;
}
}
}
这可以编译,但它在数组框中给出空指针异常。
我也尝试过类型转换器,但这也没有给我任何结果。
希望你们中的一些聪明人能告诉我去罗马的路
可以一句话找出form上所有的pictureBox
private PictureBox[] boxes;
.ctor
{
InitializeComponent();
boxes = this.Controls.OfType<PictureBox>()
.Where(pb => pb.Name.StartsWith("runeBox"))
.OrderBy(pb => int.Parse(pb.Name.Replace("runeBox","")))
.ToArray();
}
如果您只想创建它,然后手动将它们添加到表单中,您可以使用 LINQ 和 Enumerables 来发挥自己的优势
您可以像这样创建它们:
var pictureboxes = Enumerable.Range(1,48).ToList().Select(i=> new PictureBox(){Name = "runebox" + i.ToString()}).ToList();
然后添加到表单中,您只需执行以下操作即可:
pictureboxes.ForEach(pic=> this.Controls.Add(pic));
项目环境:Visual Studio 2013,使用 Windows 表单模板。
这就是我目前拥有的,我假设有一种更简单的方法来创建这样的数组。我试过很多方法,但我对C#还不够了解。
PictureBox[] boxes = {
runeBox1, runeBox2, runeBox3, runeBox4, runeBox5, runeBox6, runeBox7, runeBox8,
runeBox9, runeBox10, runeBox11, runeBox12, runeBox13, runeBox14, runeBox15, runeBox16,
runeBox17, runeBox18, runeBox19, runeBox20, runeBox21, runeBox22, runeBox23, runeBox24,
runeBox25, runeBox26, runeBox27, runeBox28, runeBox29, runeBox30, runeBox31, runeBox32,
runeBox33, runeBox34, runeBox35, runeBox36, runeBox37, runeBox38, runeBox39, runeBox40,
runeBox41, runeBox42, runeBox43, runeBox44, runeBox45, runeBox46, runeBox47, runeBox48};
到目前为止我尝试过的是:
string[] strArr = new string[48];
for (int i = 1; i != 48; i++)
{
strArr[i] = "runebox" + i.ToString();
}
int count = 0;
foreach (object p in this.Controls)
{
if (p.GetType() == typeof(PictureBox))
{
if (((PictureBox)p).Name == strArr[count])
{
boxes[count] = ((PictureBox)p);
count++;
}
}
}
这可以编译,但它在数组框中给出空指针异常。
我也尝试过类型转换器,但这也没有给我任何结果。
希望你们中的一些聪明人能告诉我去罗马的路
可以一句话找出form上所有的pictureBox
private PictureBox[] boxes;
.ctor
{
InitializeComponent();
boxes = this.Controls.OfType<PictureBox>()
.Where(pb => pb.Name.StartsWith("runeBox"))
.OrderBy(pb => int.Parse(pb.Name.Replace("runeBox","")))
.ToArray();
}
如果您只想创建它,然后手动将它们添加到表单中,您可以使用 LINQ 和 Enumerables 来发挥自己的优势
您可以像这样创建它们:
var pictureboxes = Enumerable.Range(1,48).ToList().Select(i=> new PictureBox(){Name = "runebox" + i.ToString()}).ToList();
然后添加到表单中,您只需执行以下操作即可:
pictureboxes.ForEach(pic=> this.Controls.Add(pic));