循环遍历嵌套表以获取文本框值?

时间:2016-03-12 00:04:02

标签: c# asp.net arrays multidimensional-array

我使用ASP.NET和C#制作一个基本的数独游戏。根据我的要求,我必须在代码隐藏页面中实现整个结构。

我有4个嵌套的用于创建结构的循环(包含9个嵌套表的表)。在每个内部单元格中,然后我放置一个文本框控件(总共81个)。

在玩游戏时,用户可以选择在游戏中的任何一点检查他们的进度。我需要一种循环遍历每个文本框控件并将该值存储在数组中的方法,以便我可以将其与另一个保存解决方案的9x9数组进行检查。

如何循环文本框并以逻辑方式存储它们。我宁愿将其存储为另一个9x9阵列。这将使未来的生活变得非常简单。

提前致谢!

修改

这是我的代码。我不确定它会有多大帮助。

public partial class Sudoku : System.Web.UI.Page
{
//create objects to hold our puzzle pieces
private Puzzle puzzle;
private Box box;
private SudokuTextBox textbox;
private Number number;

//create an array to hold the solution
int[,] solution;
//and one to hold the user's answers
int[,] userAnswer;


protected void Page_Load(object sender, EventArgs e)
{
    //instantiate all of our objects
    //most of them are placeholders for the loops
    puzzle  = new Puzzle();
    box     = new Box();
    textbox = new SudokuTextBox();
    number  = new Number();

    // CHOOSING A SOLUTION

    //create a new random number generator
    Random random = new Random();

    //set the solution array number to a random number from 0-10
    //(11 because the maxValue is exclusive)
    solution = chooseSolution(random.Next(1, 11));

    // CREATING THE PUZZLE

    Table structure = new Table();      //table to be the outer structure of the puzzle
    TableRow row;                       //row variable to make new rows
    TableCell cell;                     //cell variable to make new cells
    Table boxTable;                     //table that will hold individual Boxes
    TableRow boxRow;                    //row that will hold 3 SudokuTextBoxes
    TableCell boxCell;                  //cell that will hold a single SudokuTextBoxes
    TextBox input;                      //textbox that will hold the textbox in SudokuTextBox
    int answer;                         //int to hold the answer to a particular textbox

    //start looping through the puzzle rows
    for (int pr = 0; pr < 3; pr++)
    {
        row = new TableRow();           //create a new outer row

        //another loop for puzzle columns
        for(int pc = 0; pc < 3; pc++)
        {
            cell = new TableCell();         //create a new outer cell
            boxTable = new Table();         //create a new inner table
            box = puzzle.getItem(pr, pc);   //find the box at the current location in the puzzle

            //another loop for box rows
            for (int br = 0; br < 3; br++)
            {
                boxRow = new TableRow();    //create a new inner row

                //another loop for box columns
                for(int bc = 0; bc < 3; bc++)
                {
                    boxCell = new TableCell();                      //create a new inner cell
                    textbox = box.getItem(br, bc);                  //find the SudokuTextBox at the current location in the box
                    //grab the answer to this particular SudokuTextBox from the solutions array
                    answer = solution[ (pr + br + (2 * pr)), (pc + pr + (2 * pc)) ];
                    number.setNumber(answer);                       //set the Number to the found answer
                    textbox.setTextBoxValue(number);                //fill in the textbox with Number
                    input = textbox.getTextBox();                   //grab the textbox inside SudokuTextBox and store it
                    input.MaxLength = 1;                            //only allow 1 character to be typed into the textbox
                    boxCell.Controls.Add(input);                    //add the textbox to the inner cell
                    boxRow.Controls.Add(boxCell);                   //add the inner cell to the inner row
                } //end box column loop

                boxTable.Controls.Add(boxRow);  //add the inner row to the inner table

            } //end box row loop
            cell.Controls.Add(boxTable);        //add the inner table to the outer cell
            row.Controls.Add(cell);             //add the outer cell to the outer row
        } //end puzzle column loop
        structure.Controls.Add(row);            //add the outer row to the outer table
    } //end puzzle row loop

    pnlPuzzle.Controls.Add(structure);          //add the outer table to the panel on the page
}

2 个答案:

答案 0 :(得分:0)

是的,您也可以将它们存储为多维数组。

但是,由于您处于代码隐藏状态,并且文本框组件可能不会由代码生成,而是手动布局(在aspx网站上),因此您必须将它们手动放入阵列。像这样:

TextBox[][] = new TextBox[][] {
    new TextBox[] { textBox1_1, textBox1_2, textBox1_3, …, textBox1_9 },
    new TextBox[] { textBox2_1, textBox2_2, textBox2_3, …, textBox2_9 },
    …
    new TextBox[] { textBox9_1, textBox9_2, textBox9_3, …, textBox9_9 },
};

答案 1 :(得分:0)

现在已经发布了代码,很明显,如果没有每个文本框的唯一ID,您将无法重新组装数组。

TextBox类应该提供一种设置id的方法 - 我认为你想要的属性是ID - 所以我认为你的逻辑应该改为:

input = textbox.getTextBox();                   //grab the textbox inside SudokuTextBox and store it
input.MaxLength = 1;       
// Define an ID that encodes the row and column
input.ID = string.Format("txtSudukoPuzzle_{0}_{1}", br, bc)

但是我很长时间没有使用ASP.Net控件,所以不要引用我的话。一旦你获得了控件的ID,通过解析ID中的行和列,可以很容易地将结果重新组装成一个数组,可以使用@poke发布的逻辑