簡體   English   中英

在C#中清除文本框

[英]Clearing TextBoxes in C#

我正在使用ASP.NET和C#創建Sudoku游戲。 我需要使用類和繼承來完全在代碼隱藏頁面中構建結構(即,aspx頁面上沒有asp:TextBox控件)。

開始新游戲時,我的時間很糟糕,試圖清除輸入內容。 我生成了一個新的拼圖解決方案,但是我以前的盒子並沒有清除,我已經嘗試了所有可以想到的方法。

以下是與該問題有關的幾段代碼。



構建拼圖並將其存儲在Puzzle對象中的代碼。

private Puzzle newPuzzle(int[,] solution, int numbersVisible, int  maxNumbersPerBox, int maxOccurancesPerNumber)
{
    Puzzle newPuzzle = new Puzzle();
    SudokuTextBox newTextbox;
    Number newNumber;

    Random randomRC = new Random();

    //variable to hold the correct answer at the given location
    int answer;
    //variables to hold the location within the answers array
    int rowLoc;
    int colLoc;
    //counter to count the number of times we while loop
    int counter = 0;
    //variables to hold the randomly-chosen rows & col values
    int row;
    int col;
    //array to hold the positions of the numbers we are going to show
    string[] show;
    show = new string[numbersVisible];

    while(counter < numbersVisible)
    {
        //generate random numbers that gives us the location of the numbers in the solution array that we are going to show
        row = randomRC.Next(0, 9);
        col = randomRC.Next(0, 9);

        //if the random numbers are not already in the array
        if (!show.Contains(row.ToString() + ":" + col.ToString()))
        {
            //add them to the array
            show[counter] = row.ToString() + ":" + col.ToString();

            //increase the counter
            counter++;
        } //end if...contains

    } //end while


    // BUILDING THE PUZZLE
    //start looping through the puzzle rows
    for (int pr = 0; pr < 3; pr++)
    {
        //another loop for puzzle columns
        for (int pc = 0; pc < 3; pc++)
        {
            box = new Box();                //create a new Box object
            //another loop for box rows
            for (int br = 0; br < 3; br++)
            {
                //another loop for box columns
                for (int bc = 0; bc < 3; bc++)
                {
                    newTextbox = new SudokuTextBox();
                    newNumber = new Number();

                    //grab the answer to this particular SudokuTextBox from the solutions array
                    rowLoc = (pr + br + (2 * pr));
                    colLoc = (pc + bc + (2 * pc));
                    answer = solution[rowLoc, colLoc];
                    newNumber.setNumber(answer);                       //set the Number to the found answer
                    newTextbox.setTextBoxValue(newNumber);             //fill in the textbox with Number

                    //if this SudokuTextBox is chosen to be given at the start of the puzzle
                    if (show.Contains((rowLoc + ":" + colLoc).ToString()))
                    {
                        //make this SudokuTextBox visible
                        newTextbox.setVisibility(true);
                    }
                    else {
                        newTextbox.setVisibility(false);
                    } //end if

                    box.setItem(newTextbox, br, bc);                   //add the SudokuTextBox to the correct position inside Box
                } //end box column loop
            } //end box row loop
            newPuzzle.setItem(box, pr, pc);        //add the Box to the correct position inside Puzzle
        } //end puzzle column loop
    } //end puzzle row loop

    return newPuzzle;
} //end easy()



在Session中存儲新難題:

//when the Easy button is pressed
protected void btnEasy_Click(object sender, EventArgs e)
{
    //generate a new random number
    Random newRandomSoln = new Random();


    //keep picking new solutions until we get one that's different than the last one
    do
    {
        solution = chooseSolution(newRandomSoln.Next(1, 11));
    }
    while (solution == Session["solution"]);

    //store the new solution
    Session["solution"] = solution;

    //generate a new puzzle
    Session["puzzle"] = newPuzzle( (int[,])Session["solution"], 32, 4, 4 );
}



構建表結構的代碼,用存儲在Puzzle中的答案填充它,並將其添加到aspx頁面:

    ////////////////////////////////
    // 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 = new Box();                //create a new Box object

            box = ((Puzzle)Session["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 = new SudokuTextBox();                  //create a new SudokuTextBox object

                    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 = ((int[,])Session["solution"])[ (pr + br + (2 * pr)), (pc + bc + (2 * pc)) ];

                    input = textbox.getTextBox();                   //grab the textbox inside SudokuTextBox and store it
                    input.MaxLength = 1;                            //only allow 1 character to be typed into the textbox
                    //give the textbox an ID so we can find it later
                    input.ID = ("tb" + (pr + br + (2 * pr)) + "_" + (pc + bc + (2 * pc))).ToString();

                    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);

    ////////////////////////////////
    // end puzzle
    ///////////////////////////////




和SudokuTextBox類代碼:

public class SudokuTextBox
{
private System.Web.UI.WebControls.TextBox textbox;
private bool visible;
private Number number;

public SudokuTextBox()
{
    textbox = new System.Web.UI.WebControls.TextBox();
    visible = false;
    number = new Number();
} //end constructor

//function to make a new textbox
public System.Web.UI.WebControls.TextBox getTextBox()
{
    return textbox;
}

//function to get the value of a textbox
public Number getTextBoxValue()
{
    return number;
}

//????????????
public void setTextBoxValue(Number newNumber)
{
    this.number.setNumber(newNumber.getNumber());
}

//function to get the visibility of a textbox
public bool getVisibility()
{
    return visible;
}

//function to change the visibility of a textbox
public void setVisibility(bool newVisible)
{
    if (newVisible)
    {
        //if the textbox is visible
        //get the number
        //and make it disabled
        textbox.Text = number.getNumber().ToString();
        textbox.ReadOnly = true;
        textbox.BackColor = System.Drawing.Color.FromArgb(150, 148, 115);
    } else
    {
        //if it is not visible
        //hide the number
        //and make it enabled
        textbox.ReadOnly = false;
        textbox.Text = "";
        textbox.BackColor = System.Drawing.Color.White;
    }
}

//function to change the color of the textbox if it is wrong
public void setWrongNumber()
{
    textbox.BackColor = System.Drawing.Color.FromArgb(5, 156, 202, 252);
}

//function to change the color of the textbox if it is correct
public void setCorrectNumber()
{
    //but don't change disable text boxes
    if(textbox.ReadOnly != true)
    {
        textbox.BackColor = System.Drawing.Color.White;
    }
}

//function to change the color of the textbox if it is blank
public void setBlankNumber()
{
    //but don't change disable text boxes
    if (textbox.ReadOnly != true)
    {
        textbox.BackColor = System.Drawing.Color.White;
    }
}

//function to show the value of a textbox when clicking the "Hint" button
//also changes the color of the textbox so we know it was shown with a hint
public void setHint()
{
    setVisibility(true);
}

} //end class

謝謝所有給您兩分錢的人。 我只是想讓大家知道我已經解決了這個問題:

最后,我分離出了​​構建表結構的代碼,將其從Page_Load中取出並放入了自己的函數中。 然后,我從Page_Load調用了此函數。 當我單擊“新拼圖”按鈕時,我也調用了此功能。 我還添加了與上面評論類似的邏輯,以在構建新表之前清除先前的表結構:

foreach(Control c in pnlPuzzle.Controls){

    pnlPuzzle.Controls.Remove(c);
}

我不確定為什么能解決我的問題,但確實有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM