将文本框放入数组中
Put textboxes in an array
我有大约 80 个文本框,我将它们命名为
s1v1, s1v2, ..... s12v7.
但我想将它们放在一个数组中或列出它们,以便我可以在 for 循环中轻松使用它们 我使用下面的数组,但我相信有更简单的方法可以做到这一点。你能告诉我最好的解决方法是什么吗?
TextBox[,] girdiler = new TextBox[,]{
{ s1v1, s1v2, s1v3, s1v4, s1v5, s1v6, s1v7 },
{ s2v1, s2v2, s2v3, s2v4, s2v5, s2v6, s2v7 },
{ s3v1, s3v2, s3v3, s3v4, s3v5, s3v6, s3v7 },
{ s4v1, s4v2, s4v3, s4v4, s4v5, s4v6, s4v7 },
{ s5v1, s5v2, s5v3, s5v4, s5v5, s5v6, s5v7 },
{ s6v1, s6v2, s6v3, s6v4, s6v5, s6v6, s6v7 },
{ s7v1, s7v2, s7v3, s7v4, s7v5, s7v6, s7v7 },
{ s8v1, s8v2, s8v3, s8v4, s8v5, s8v6, s8v7 },
{ s9v1, s9v2, s9v3, s9v4, s9v5, s9v6, s9v7 },
{ s10v1, s10v2, s10v3, s10v4, s10v5, s10v6, s10v7 },
{ s11v1, s11v2, s11v3, s11v4, s11v5, s11v6, s11v7 },
{ s12v1, s12v2, s12v3, s12v4, s12v5, s12v6, s12v7 }
};
如您所见,它太长了,我想让它更简单一些。
您可以在 Linq 的帮助下查询 Controls
以便将所有 TextBox
es 组织成一个集合:
using System.Linq;
...
var boxes = Controls
.OfType<TextBox>()
.Where(box => !string.IsNullOrEmpty(box.Name)) // to be on the safe side
.ToDictionary(box => box.Name, box => box);
TextBox[,] girdiler = new TextBox[12, 7];
for (int s = 1; s <= girdiler.GetLength(0); ++s)
for (int v = 1; v <= girdiler.GetLength(0); ++v)
girdiler[s - 1, v - 1] = boxes[$"s{s}v{v}"];
我有大约 80 个文本框,我将它们命名为
s1v1, s1v2, ..... s12v7.
但我想将它们放在一个数组中或列出它们,以便我可以在 for 循环中轻松使用它们 我使用下面的数组,但我相信有更简单的方法可以做到这一点。你能告诉我最好的解决方法是什么吗?
TextBox[,] girdiler = new TextBox[,]{
{ s1v1, s1v2, s1v3, s1v4, s1v5, s1v6, s1v7 },
{ s2v1, s2v2, s2v3, s2v4, s2v5, s2v6, s2v7 },
{ s3v1, s3v2, s3v3, s3v4, s3v5, s3v6, s3v7 },
{ s4v1, s4v2, s4v3, s4v4, s4v5, s4v6, s4v7 },
{ s5v1, s5v2, s5v3, s5v4, s5v5, s5v6, s5v7 },
{ s6v1, s6v2, s6v3, s6v4, s6v5, s6v6, s6v7 },
{ s7v1, s7v2, s7v3, s7v4, s7v5, s7v6, s7v7 },
{ s8v1, s8v2, s8v3, s8v4, s8v5, s8v6, s8v7 },
{ s9v1, s9v2, s9v3, s9v4, s9v5, s9v6, s9v7 },
{ s10v1, s10v2, s10v3, s10v4, s10v5, s10v6, s10v7 },
{ s11v1, s11v2, s11v3, s11v4, s11v5, s11v6, s11v7 },
{ s12v1, s12v2, s12v3, s12v4, s12v5, s12v6, s12v7 }
};
如您所见,它太长了,我想让它更简单一些。
您可以在 Linq 的帮助下查询 Controls
以便将所有 TextBox
es 组织成一个集合:
using System.Linq;
...
var boxes = Controls
.OfType<TextBox>()
.Where(box => !string.IsNullOrEmpty(box.Name)) // to be on the safe side
.ToDictionary(box => box.Name, box => box);
TextBox[,] girdiler = new TextBox[12, 7];
for (int s = 1; s <= girdiler.GetLength(0); ++s)
for (int v = 1; v <= girdiler.GetLength(0); ++v)
girdiler[s - 1, v - 1] = boxes[$"s{s}v{v}"];