我的表单如下所示:
x, y, A,B,C
是矩阵。 x
右侧的文本框被命名为 x1,...,x6
,A
右侧的文本框被命名为
a11,...,a16
...
a61, ... ,a66
它们都是整数。我想要做的是将这些值放入数组中,例如:
x=(20,...,756);
将 A 放入二维数组,如 a[1][1]=932
... a[6][6]=666
。
如果是,怎么办?与分组框?我不知道如何解决这个问题。提前谢谢
最佳答案
看到您的控件的名称嵌入了矩阵位置,我们可以编写
这里没有错误检查,我假设数字总是整数......
int[] xMatrix = new int[6];
int[,] aMatrix = new int[6,6];
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
string pos = control.Name.SubString(1);
if(control.Name.StartsWith("a"))
{
int matrixPos = Convert.ToInt32(pos) ;
int x = (matrixPos / 10) - 1;
int y = (matrixPos % 10) - 1;
aMatrix[x,y] = Convert.ToInt32(control.Text);
}
else if(control.Name.StartsWith("x")
{
int arrayPos = Convert.ToInt32(pos) - 1;
xMatrix[arrayPos] = Convert.ToInt32(control.Text);
}
}
}
关于c# - 文本框值放入 int 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353587/