Esempio n. 1
0
        private void SudokuApp_Load(object sender, EventArgs e)
        {
            //Název formuláře
            Text = "Hra - " + name;
            //Nastavení vybraného pole
            if (selectedField.X == -1 && selectedField.Y == -1)
            {
                selectedField = board.FirstEmpty();
            }

            for (int gridCol = 0; gridCol < 3; gridCol++)
            {
                for (int gridRow = 0; gridRow < 3; gridRow++)
                {
                    TableLayoutPanel panel = new TableLayoutPanel();
                    panel.Name        = "Grid_" + gridCol + "_" + gridRow;
                    panel.RowCount    = 3;
                    panel.ColumnCount = 3;

                    panel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33f));
                    panel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33f));
                    panel.RowStyles.Add(new RowStyle(SizeType.Percent, 33.33f));

                    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33f));
                    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33f));
                    panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33.33f));

                    panel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
                    panel.Width           = sudokuBoard.Width / 3;
                    panel.Height          = sudokuBoard.Height / 3;

                    panel.CellPaint += OnCellPaint;
                    sudokuBoard.Controls.Add(panel, gridCol, gridRow);
                }
            }

            //Načtení pole z dat
            for (int col = 0; col < 9; col++)
            {
                for (int row = 0; row < 9; row++)
                {
                    Label label = new Label()
                    {
                        Name = col + "_" + row
                    };

                    int val = board.GetValue(col, row);

                    label.ForeColor = (originalBoard.GetValue(col, row) != 0) ? Color.Black : Color.Blue;
                    label.Text      = val == 0 ? "" : "" + val;
                    label.TextAlign = ContentAlignment.MiddleCenter;

                    label.Font   = new Font(label.Font.FontFamily, 20);
                    label.Size   = new Size(40, 40);
                    label.Click += PrekliknutiPole;

                    if (col == selectedField.X && row == selectedField.Y)
                    {
                        label.BackColor = Color.Yellow;
                    }

                    int gridCol = col / 3;
                    int gridRow = row / 3;
                    ((TableLayoutPanel)sudokuBoard.Controls["Grid_" + gridCol + "_" + gridRow]).Controls.Add(label, col % 3, row % 3);
                }
            }

            //Překreslení špatně zadaných polí
            RedrawFields(true);
        }