9 Replies - 6510 Views - Last Post: 27 June 2011 - 04:06 PM Rate Topic: -----

#1 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • Posts: 378
  • Joined: 01-December 08

TextBox Text Field Isn't Updating

Posted 27 June 2011 - 11:06 AM

I have an array of TextBoxes and a single Button. When the button is clicked, it takes the text from the TextBoxes and puts it into an array. It then solves the problem and prints it. However, when I try to print it by setting the TextBoxes' text to the corresponding data from the finished array, the text doesn't change on screen. The weird thing is that the line below in which Console.WriteLine outputs the text of the TextBoxes (added for testing purposes) shows that the text does contain the right number, it just doesn't show up in the GUI.
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                fields[i,j] = new TextBox();
            }
        }
        int xPos = 8;
        int yPos = 8;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                fields[i, j].Height = 20;
                fields[i, j].Width = 20;
                fields[i, j].MaxLength = 1;
                fields[i, j].Left = xPos;
                fields[i, j].Top = yPos;
                fields[i, j].Text = "";
                xPos += 25;
            }
            xPos = 8;
            yPos += 25;
        }
        solveButton.Text = "Solve";
        solveButton.Width = 100;
        solveButton.Left = 75;
        solveButton.Top = 235;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                Controls.Add(fields[i,j]);
            }
        }
        Controls.Add(solveButton);
        solveButton.Click += new System.EventHandler(solveButton_Click);



    public static void printPuzzle(int[,] board)
    {
        for (int a = 0; a < 9; a++) {
            for (int b = 0; b < 9; b++) {
                fields[a, b].Text = board[a, b].ToString();
                Console.WriteLine(fields[a, b].Text);
            }
        }
    }



    private void solveButton_Click(object sender, System.EventArgs e)
    {
        populateArray();
        solve();
    }


Edit: On a side note, the button disappears after I click it... Not sure if the problems are related, but ask if you need to see more code.

This post has been edited by ericr2427: 27 June 2011 - 11:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: TextBox Text Field Isn't Updating

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 11:25 AM

Well, we'd have to see more code. What you've shown so far is effectively OK, with some extrapolations on my part. I had to just assume what was going on in the methods you haven't shown us.

In the code you provided, you're not calling printPuzzle, so we can't see what's actually happening. Show us all the related code. Also, I'm guessing that the code you provided not in a function is in the constructor or the Load event?
Was This Post Helpful? 0
  • +
  • -

#3 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • Posts: 378
  • Joined: 01-December 08

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 12:12 PM

Here's the entire code.
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
class node
{
    public node right;
    public node left;
    public node up;
    public node down;
    public node colHead;
    public int numNodes;
    public int nodeID;
}
class sudokuSolver : Form
{
    static int[,,] puzzles = new int[3,9,9] {{
        {0,0,0,3,9,0,5,0,0},
        {8,2,7,0,0,0,0,0,9},
        {0,3,0,7,0,0,1,4,0},
        {1,0,2,5,7,0,0,0,3},
        {0,0,8,2,0,4,9,0,0},
        {7,0,0,0,6,1,4,0,5},
        {0,6,4,0,0,8,0,3,0},
        {9,0,0,0,0,0,8,5,6},
        {0,0,5,0,1,7,0,0,0}
    },{
        {7,9,0,0,0,0,3,0,0},
        {0,0,0,0,0,6,9,0,0},
        {8,0,0,0,3,0,0,7,6},
        {0,0,0,0,0,5,0,0,2},
        {0,0,5,4,1,8,7,0,0},
        {4,0,0,7,0,0,0,0,0},
        {6,1,0,0,9,0,0,0,8},
        {0,0,2,3,0,0,0,0,0},
        {0,0,9,0,0,0,0,5,4}
    },{
        {0,0,5,0,9,0,0,0,2},
        {0,0,0,0,0,4,0,0,7},
        {0,0,6,0,0,0,1,9,0},
        {0,0,2,0,4,8,0,0,0},
        {8,0,0,3,0,6,0,0,5},
        {0,0,0,1,2,0,3,0,0},
        {0,5,7,0,0,0,6,0,0},
        {9,0,0,7,0,0,0,0,0},
        {1,0,0,0,8,0,2,0,0}
    }};
    static int[,] puzzle = new int[9, 9];
    public static node root = new node();
    public static node[] headers = new node[324];
    public static node[] nodes;
    public static TextBox[,] fields = new TextBox[9,9];
    public static Button solveButton = new Button();
    public sudokuSolver()
    {
        this.Text = "Sudoku Solver";
        this.Width = 254;
        this.Height = 300;
        this.MinimumSize = new Size(254, 300);
        this.MaximumSize = new Size(254, 300);
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                fields[i,j] = new TextBox();
            }
        }
        int xPos = 8;
        int yPos = 8;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                fields[i, j].Height = 20;
                fields[i, j].Width = 20;
                fields[i, j].MaxLength = 1;
                fields[i, j].Left = xPos;
                fields[i, j].Top = yPos;
                fields[i, j].Text = "";
                xPos += 25;
            }
            xPos = 8;
            yPos += 25;
        }
        solveButton.Text = "Solve";
        solveButton.Width = 100;
        solveButton.Left = 75;
        solveButton.Top = 235;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                Controls.Add(fields[i,j]);
            }
        }
        Controls.Add(solveButton);
        solveButton.Click += new System.EventHandler(solveButton_Click);
    }
    public static void Main()
    {
        sudokuSolver solver = new sudokuSolver();
        Application.Run(solver);
    }
    public static void solve()
    {
        addHeaders();
        constructMatrix();
        ArrayList path = new ArrayList();
        sudokuSolver solver = new sudokuSolver();
        solver.recursiveSolve(path);
    }
    public static void addHeaders()
    {
        for (int i = 0; i < 324; i++)
        {
            headers[i] = new node();
            if (i == 0)
            {
                headers[i].left = root;
                root.right = headers[i];
            }
            else
            {
                headers[i - 1].right = headers[i];
                headers[i].left = headers[i-1];
            }
            headers[i].up = headers[i];
            headers[i].down = headers[i];
            headers[i].numNodes = 0;
            headers[i].nodeID = i;
        }
        headers[323].right = root;
        root.left = headers[323];
    }
    public static void constructMatrix()
    {
        int numNodes = 0;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (puzzle[i,j] == 0)
                {
                    numNodes += 9;
                }
                else
                {
                    numNodes++;
                }
            }
        }
        numNodes *= 4;
        nodes = new node[numNodes];
        for (int i = 0; i < numNodes; i++)
        {
            nodes[i] = new node();
        }
        int currentNode = 0, start, end;
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                if (puzzle[i,j] == 0)
                {
                    start = 1;
                    end = 9;
                }
                else
                {
                    start = puzzle[i,j];
                    end = start;
                }
                for (int x = start; x <= end; x++)
                {
                    int[] values = {j+(9*i), x+(9*i)+80, x+(9*j)+161, x+(9*boxNum(i,j))+242};
                    for (int c = 0; c < 4; c++, currentNode++)
                    {
                        node current = nodes[currentNode];
                        if (c != 0)
                        {
                            current.left = nodes[currentNode-1];
                        }
                        else
                        {
                            current.left = nodes[currentNode+3];
                        }
                        if (c != 3)
                        {
                            current.right = nodes[currentNode+1];
                        }
                        else
                        {
                            current.right = nodes[currentNode-3];
                        }
                        current.up = headers[values[c]].up;
                        headers[values[c]].up.down = current;
                        headers[values[c]].up = current;
                        current.down = headers[values[c]];
                        current.colHead = headers[values[c]];
                        current.colHead.numNodes++;
                        current.nodeID = currentNode;
                    }
                }
            }
        }
    }
    public static int boxNum (int row, int col)
    {
        int add = 0;
        if (row/3 == 1)
            add = 3;
        if (row/3 == 2)
            add = 6;
        if (col/3 == 0)
            return add + 0;
        if (col/3 == 1)
            return add + 1;
        else
            return add + 2;
    }
    public bool recursiveSolve(ArrayList path)
    {
        if (root.right.Equals(root))
        {
            printSolution(path);
            return true;
        }
        int columnNo = chooseColumn();
        coverColumn(headers[columnNo]);
        for (node i = headers[columnNo].down; !i.Equals(headers[columnNo]); i = i.down)
        {
            ArrayList newPath = path;
            newPath.Add(i);
            for (node j = i.right; !j.Equals(i); j = j.right)
            {
                coverColumn(j.colHead);
            }
            if (recursiveSolve(newPath)) {
                return true;
            }
            for (node j = i.left; !j.Equals(i); j = j.left)
            {
                uncoverColumn(j.colHead);
            }
        }
        uncoverColumn(headers[columnNo]);
        return false;
    }
    public static void printSolution(ArrayList path)
    {
        int[,] solution = new int[9,9];
        foreach (node i in path)
        {
            node start = i;
            while (start.left.nodeID < start.nodeID)
            {
                start = start.left;
            }
            int rc = start.colHead.nodeID;
            int y = rc / 9;
            int x = rc - (y * 9);
            int rv = start.right.colHead.nodeID;
            int v = rv - (y * 9) - 80;
            solution[y,x] = v;
        }
        printPuzzle(solution);
    }
    public static int chooseColumn()
    {
        int lowest = 729;
        int lowestCol = 0;
        for (node i = root.right; !i.Equals(root); i = i.right)
        {
            if (i.numNodes < lowest)
            {
                lowest = i.numNodes;
                lowestCol = i.nodeID;
            }
        }
        return lowestCol;
    }
    public static void coverColumn (node header)
    {
        header.left.right = header.right;
        header.right.left = header.left;
        for (node i = header.down; !i.Equals(header); i = i.down)
        {
            for (node j = i.right; !j.Equals(i); j = j.right)
            {
                j.down.up = j.up;
                j.up.down = j.down;
                j.colHead.numNodes--;
            }
        }
    }
    public static void uncoverColumn (node header)
    {
        for (node i = header.up; !i.Equals(header); i = i.up)
        {
            for (node j = i.left; !j.Equals(i); j = j.left)
            {
                j.colHead.numNodes++;
                j.down.up = j;
                j.up.down = j;
            }
        }
        header.left.right = header.right;
        header.right.left = header.left;
    }
    public static void populateArray() {
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                //if (fields[i, j].Text == " " || fields[i, j].Text == "")
                //{
                //    puzzle[i, j] = 0;
                //}
                //else
                //{
                //    puzzle[i, j] = int.Parse(fields[i,j].Text);
                //}
                puzzle[i, j] = puzzles[0, i, j];
            }
        }
    }
    public static void printPuzzle(int[,] board)
    {
        for (int a = 0; a < 9; a++) {
            for (int b = 0; b < 9; b++) {
                fields[a, b].Text = board[a, b].ToString();
                Console.WriteLine(fields[a, b].Text);
            }
        }
    }
    private void solveButton_Click(object sender, System.EventArgs e)
    {
        populateArray();
        solve();
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 foofo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 19
  • Joined: 06-May 11

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 12:58 PM

Maybe you forgot that all important this.Invalidate()...
Was This Post Helpful? 0
  • +
  • -

#5 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • Posts: 378
  • Joined: 01-December 08

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 01:09 PM

Nope, still not showing up. I still have no idea why this isn't working.
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 02:58 PM

Hi,

This really illustrates why you shouldn't make all your members static :)

I shall go through what I think is happening step by step:

First, you start the program, and this code runs:

public static void Main() {
        sudokoSolver solver = new sudokuSolver();
        Application.Run(solver);
}



You create an instance of your form (this instance is local to Main()). By calling Run(), the form is then made visible for you to see. No problem.

However, you then click the solve button, of which calls this method called solve():

public static void solve() {
        addHeaders();
        constructMatrix();
        ArrayList path = new ArrayList();
        sudokoSolver solver = new sudokuSolver();
        solver.recursiveSolve(path);
}



Look at this line; sudokoSolver solver = new sudokuSolver();. You create another new instance of your form (this one is local to the solve() method). You do not see this new form as it is hidden because you don't call .Show() on it.

You then call your recursiveSolve() method on this new form instance. Therefore, all the GUI updates that ultimately get carried out as a result of the recursiveSolve() call are done on the new, hidden form that you have just created in that solve() method. Hence, you don't see any updates on the form you created on Main(), as they are all been done on this invisible new form :)

Putting this line solver.Show(); after you create the new form instance in the solve() method should make things a bit clearer for you.



You may wonder why the TextBox array ('fields') isn't shared across both instances of your Form (as that is what static is supposed to do)? Well, the array variable is. However, you create new instances of the textbox class, and add these individual new instances to the new form you create in the solve() method. Therefore, each form has distinct textbox instances added to their Control collection, and so are independent of each other in terms of the textboxes.

As for the button disappearing, you create a SINGLE static instance of that button (different from the textboxes), and so that same instance is being shared across BOTH forms. When you call Add(), it checks to see if the button instance's parent (your original form in this case) is null. If it isn't (it isn't in your case), then it removes the button from that form's control collection before adding it to the new form's control collection. Hence, it disappears from the first form.


You really need to make all the members instance members (especially the controls; it makes very little sense to have static controls), and just use the single form instance you create in Main(). It will continue to give you headaches otherwise, trust me :)

This post has been edited by CodingSup3rnatur@l-360: 27 June 2011 - 03:47 PM

Was This Post Helpful? 3
  • +
  • -

#7 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • Posts: 378
  • Joined: 01-December 08

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 03:40 PM

Thanks a bunch, I figured the code was probably horribly wrong in some way :). Anyhow, it's working great now, I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 03:42 PM

Very clear explanation, CodingSup3rnatur@l-360 (anyone tell you your name is a bitch to type? :P)

I'd guess that OP isn't making this with Visual Studio, no? Usually, the creation of a form instance is handled by VS, generated in a file called Program.cs. It makes it somewhat easier to avoid this kind of problem.
Was This Post Helpful? 0
  • +
  • -

#9 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • Posts: 378
  • Joined: 01-December 08

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 03:49 PM

I'd already written the rest of the code as a console version and I just sort of wrote the GUI on top of it by hand. Add that to my complete inexperience with C# (~2 days) and you get this kind of code I guess.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,903
  • Joined: 08-June 10

Re: TextBox Text Field Isn't Updating

Posted 27 June 2011 - 04:06 PM

Makes sense.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1