文本框的for循环 [英] for loop for textboxes
问题描述
我的表单中有100个文本框,我想将文本框的文本确定为10 * 10矩阵.
我该如何使用for循环?
我的意思是例如
for ( int i = 0 ;我< 100 ; i ++){
{
for ( int j = 0 ; j < 100 ; j ++)
matrix [i,j] = float .Parse (TextBox [i,j] .text);
}
但是C#没有这样的东西,请帮助我:confused:
您可以尝试这样的事情.将您的文本框ID设置为类似于textBox_< row> _< column>如textBox_1_1,textBox_1_2
您的代码将如下所示.
for (int i = 0; i < 10; i++){ { for (int j = 0; j < 10; j++) matrix[i, j] =( (TextBox) FindControl(string.Format("textBox_{0}_{1}", i, j))).Text; }
您还可以将文本框的ID与它们的行列索引相对应地存储在集合中,并在需要时从集合中获取ID.我只是用C#敲了一下,就可以正常工作(每天学习一些新知识!)
创建一个带有两个按钮的表单;一个创建,一个输出到调试窗口;
这是用于创建100个名为TextBox0
到TextBox99
的文本框并将其文本设置为0到99之间的值以及矩阵类变量的代码.然后它将搜索每个文本框的表单控件collectin并将其值加载到矩阵中.
// 为文本框值创建新矩阵 私有 int [,] matrix = 新 int [ 10 , 10 ]; 公共 无效 create100textboxes() { // 创建100个文本框,将文本设置为文本框编号 for ( int i = 0 ; i < 100 ; i ++) { TextBox newTextbox = 新 TextBox(); newTextbox.Name = " + i.ToString(); newTextbox.Text = i.ToString(); 此 .Controls.Add(newTextbox); newTextbox.BringToFront(); } int textBoxCount = 0 ; // 创建行循环 for ( int r = 0 ; r < 10 ; r ++) { // 创建col循环 for ( int c = 0 ; c < 10 ; c ++) { TextBox currentTextBox =(TextBox)控件[Controls.IndexOfKey(" + textBoxCount.ToString()) ]; matrix [c,r] = int .Parse(currentTextBox.Text); textBoxCount ++; } } }
下面的代码将遍历矩阵,并将每行/列地址输出到调试窗口;
私有 无效 buttonPrint_Click(对象发件人,EventArgs e) { for ( int r = 0 ; r < 10 ; r ++) { for ( int c = 0 ; c < 10 ; c ++) { System.Diagnostics.Debug.Print(" + r + ," + c + + matrix [r,c]); } } }
I have 100 textboxes in my form and I want to determine my textboxes'' text to a 10*10 matrix.
How can I do this with for loops?
I mean for example
for (int i = 0; i < 100; i++){
{
for (int j = 0; j < 100; j++)
matrix[i, j] = float.Parse(TextBox[i,j].text);
}
but c# dosn''t have somthing like this, please help me:confused:
You can try something like this. Set your textbox id something like this textBox_<row>_<column> like textBox_1_1, textBox_1_2
and your code will look like this.
for (int i = 0; i < 10; i++){ { for (int j = 0; j < 10; j++) matrix[i, j] =( (TextBox) FindControl(string.Format("textBox_{0}_{1}", i, j))).Text; }
You can also store the id of the textboxes against their row column index in a collection and fetch the id from the collection when required.
Try this; i just knocked this up in C# and works fine (learn something new everyday!)
create a form with 2 buttons on it; One does the create, and one does the output to debug window;
This is the code to create 100 textboxes namedTextBox0
toTextBox99
and set their text to a value from 0 to 99 and also the matrix class variable. It will then search the forms controls collectin for each textbox and load its value to the matrix.
//Create a new matrix for the textbox values private int[,] matrix = new int[10, 10]; public void create100textboxes() { // Create 100 textboxes, set text to the textbox number for (int i = 0; i < 100; i++) { TextBox newTextbox = new TextBox(); newTextbox.Name = "TextBox" + i.ToString(); newTextbox.Text = i.ToString (); this.Controls.Add(newTextbox); newTextbox.BringToFront(); } int textBoxCount = 0; //Create the row loop for (int r = 0; r < 10; r++) { //Create the col loop for (int c = 0; c < 10; c++) { TextBox currentTextBox = (TextBox) Controls[Controls.IndexOfKey("TextBox" + textBoxCount.ToString())]; matrix[c,r] = int.Parse(currentTextBox.Text); textBoxCount++; } } }
The code below will iterate through the matrix and output to the debug window each row/col address;
private void buttonPrint_Click(object sender, EventArgs e) { for (int r = 0; r < 10; r++) { for (int c = 0; c < 10; c++) { System.Diagnostics.Debug.Print("Matrix[" + r + "," + c + "]=" + matrix[r, c]); } } }
这篇关于文本框的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!