c# Windows窗体-如何使文本框数组可变
本文关键字:数组 文本 何使 Windows 窗体 | 更新日期: 2023-09-27 18:12:15
int iAcessorioContar = 10;
for (int iContador = 2; iContador <= iAcessorioContar; iContador++)
{
Label lblAcessorio = (Label)gpbCategoria.Controls.Find("lblAcessorio" + iContador, false).FirstOrDefault();
TextBox txtAcessorio = (TextBox)gpbCategoria.Controls.Find("txtAcessorio" + iContador, false).FirstOrDefault();
}
我想在变量lblAcessorio和txtAcessorio数组中存储所有文本框和标签值,这样我就可以在我的代码中引用(所有这些标签和文本框将在运行时创建,因为我需要这个。
谢谢
你似乎在问如何把你得到的东西放入数组。所以,这里有一些代码展示了如何使用数组,它应该为你指明正确的方向。
[TestMethod]
public void GenericTest()
{
object[,] array = new object[9, 2];
for (int i = 0; i <= 8; i++)
{
array[i, 0] = i;
array[i, 1] = i * 2;
}
for (int i = 0; i < array.GetLength(0); i++)
{
for (int k = 0; k < array.GetLength(1); k++)
{
Console.Write(array[i,k]+" ");
}
Console.WriteLine();
}
}
0 0
2
2
6
8
5 10
12
7 14
8 16
创建并添加列表:
// declare these at CLASS level (not in the method that has the code below)
List<Label> Labels = new List<Label>();
List<TextBox> TextBoxes = new List<TextBox>();
...
// in some method:
int iAcessorioContar = 10;
for (int iContador = 2; iContador <= iAcessorioContar; iContador++)
{
Label lblAcessorio = (Label)gpbCategoria.Controls.Find("lblAcessorio" + iContador, false).FirstOrDefault();
TextBox txtAcessorio = (TextBox)gpbCategoria.Controls.Find("txtAcessorio" + iContador, false).FirstOrDefault();
Labels.Add(lblAcessorio);
TextBoxes.Add(txtAcessorio);
}