开发者

C# Assign to an Array with many NumericUpdown Objects

开发者 https://www.devze.com 2023-03-25 08:17 出处:网络
Ok so I am making a sudoku solver for fun (yes I know it\'s already been made many times over) so to let people input there values before solving I used numericalupdown (81 one of them to be exact)

Ok so I am making a sudoku solver for fun (yes I know it's already been made many times over) so to let people input there values before solving I used numericalupdown (81 one of them to be exact) and i wanted to assign all of them to an array:

        int[,] Sudoku = new int[9, 9];

and then on clicking "solve" the first thing it's supposed to do is put all the values in the array:

        private void button1_Click(object sender, EventArgs e)
    {
        for (int x = 0; x < 81; x++)
        {
            for (int y = 0; y < 9; y++)
            {
                if (x-1 == 0)
                {
                 开发者_开发知识库   Sudoku[x - 1, y - 1] = Convert.ToInt32(numericUpDown[y].Value);
                }
                else
                {
                    Sudoku[x - 1, y - 1] = Convert.ToInt32(numericUpDown[x][y].Value);
                }
            }
        }

    }

obviously you can't do: "numbericupdown[y]" but thats for you to see what I am trying to do....

sooooo thoughts?

THANKS, Craiggles


If you put your numericUpDown controls into a 9x9 grid just like you have for the results, then copying the values will be straightforward.

private void button1_Click(object sender, EventArgs e)
{
    for (int x = 0; x < 9; x++)
    {
        for (int y = 0; y < 9; y++)
        {
            Sudoku[x, y] = Convert.ToInt32(numericUpDown[x, y].Value);
        }
    }

}

If the controls are actually all dropped onto the form, then don't do that, remove them from the form. Use code to create them, place them programmatically, and put them into a 2d array. Since this is a learning program anyways, that's good practice for doing GUI's programmatically instead of just by drag-n-drop.


I would go about constructing/placing them dynamically and at the same time putting them into an array or list, but if they are already laid out onto a form you can just add them to a generic list to iterate over them.

List<NumericUpDown> nums = new List<NumericUpDown>();
nums.add(numericUpDwown1);

then you could calculate nums[1]*nums[9]... or whatever


I think I understand what you are doing... Two suggestions: 1) put all of your controls into an array of NumericaUpDown objects. Then you could do numericUpDown[y].Value. Otherwise, 2) name the controls ending with numbers (or some similar convention) then use reflection to find the right one and get its value.


Actually there is no reason at all why you cannot create controls and store them in various collection classes - not just arrays.

I would consider putting these into some sort of structured set of collections that reflect the structure of a sudoku grid.

You could dynamically create and place the controls on the form as well as holding them in the collection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号