private void CreateGrid(int num) { pnlPuzzle.Controls.Clear(); Label[] buttonlabels = new Label[num]; int boxcount = Convert.ToInt32(Math.Sqrt(num)); int width = pnlPuzzle.Width / boxcount; int height = pnlPuzzle.Height / boxcount; int left = 0; int top = 0; int count = 0; int y_coord = 0; int x_coord = 0; //if (resize == false) //{ numbers = GenerateNumbers(num); //} for (int x = 0; x <= buttonlabels.GetUpperBound(0); x++) { buttonlabels[x] = new Label(); buttonlabels[x].Click += LabelClick; buttonlabels[x].DragOver += LabelDragOver; buttonlabels[x].MouseDown += LabelMouseDown; buttonlabels[x].DragDrop += LabelDragDrop; buttonlabels[x].Font = new Font(FontFamily.GenericSansSerif, 42.0f, FontStyle.Bold); buttonlabels[x].TextAlign = ContentAlignment.MiddleCenter; buttonlabels[x].AllowDrop = true; if (x== buttonlabels.GetUpperBound(0)) { buttonlabels[x].BorderStyle = BorderStyle.None; buttonlabels[x].BackColor = Color.DarkGray; } else { buttonlabels[x].BorderStyle = BorderStyle.Fixed3D; buttonlabels[x].BackColor = Color.White; } buttonlabels[x].Width = width; buttonlabels[x].Height = height; if (count == 0) { buttonlabels[x].Location = new Point(0, top); } else { if (count == boxcount) { left = 0; top += height; count = 0; buttonlabels[x].Location = new Point(0, top); y_coord++; x_coord = 0; } else { buttonlabels[x].Location = new Point(left + width, top); left += width; x_coord++; } } count++; buttonlabels[x].Tag = x_coord.ToString() + "," + y_coord.ToString(); if (x < buttonlabels.GetUpperBound(0)) { buttonlabels[x].Text = numbers[x].ToString(); } pnlPuzzle.Controls.Add(buttonlabels[x]); } }