【发布时间】:2020-05-14 10:38:55
【问题描述】:
我创建一个大小取决于用户的矩阵,我生成随机数。 问题是如何从文本框数组中获取所有值并将它们写入另一个数组。 或者也许有更合理的方法?但事实是我需要能够在行和列中“行走”。创建矩阵的表格
public void gen__matrix()
{
int row = Convert.ToInt32(txt_row.Text);
int col = Convert.ToInt32(txt_col.Text);
int[,] ar_matrix = new int[row, col];
addTextBox(row, col, ar_matrix);
}
TextBox 矩阵本身的值
public void addTextBox(int row, int col, int[,] ar_matrix)
{
Random rnd = new Random();
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
double rnd_val = rnd.Next(1, col + 1);
TextBox textboxEdit = new TextBox();
int pos_x_lb = 10;
int pos_x_lb_step = 85;
int pos_y_lb = 10;
int pos_y_lb_step = 30;
textboxEdit.Size = new Size(80, 20);
textboxEdit.Name = Convert.ToString(row + i);
textboxEdit.Text = Convert.ToString(Math.Round(rnd_val, 3));
textboxEdit.Font = new Font(textboxEdit.Font.FontFamily, 12);
textboxEdit.TextAlign = HorizontalAlignment.Center;
textboxEdit.Location = new Point(pos_x_lb + (j * pos_x_lb_step), pos_y_lb + (pos_y_lb_step * i));
panel1.Controls.Add(textboxEdit);
}
}
}
【问题讨论】:
-
如果你给你的文本框赋值
Name属性,比如textboxEdit.Name = "t_" + i + "_" + j;,你可以得到(TextBox)panel1.Controls.Find("t_" + i + "_" + j);的任意文本框 -
有些想法或什么 for (int i = 0; i
-
prnt.sc/sgngco 我不明白为什么会出错
标签: c# arrays multidimensional-array textbox