private void Switch(MyPB box1, MyPB box2) { int tmp = box2.ImageIndex; box2.Image = images[box1.ImageIndex]; box2.ImageIndex = box1.ImageIndex; box1.Image = images[tmp]; box1.ImageIndex = tmp; }
private void Startgame() { if (pbWhole != null) { pbPuzzle.Controls.Remove(pbWhole); pbWhole.Dispose(); pbWhole = null; } if (picBoxes == null) { picBoxes = new PictureBox[currentLevel]; images = new Image[currentLevel]; } int numRow = (int)Math.Sqrt(currentLevel); int numCol = numRow; int x = pbPuzzle.Width / numRow; int y = pbPuzzle.Height / numCol; int[] indice = new int[currentLevel]; for (int i = 0; i < currentLevel; i++) { indice[i] = i; if (picBoxes[i] == null) { picBoxes[i] = new MyPB(); picBoxes[i].Click += new EventHandler(xephinhClick); picBoxes[i].BorderStyle = BorderStyle.Fixed3D; } picBoxes[i].Width = x; picBoxes[i].Height = y; ((MyPB)picBoxes[i]).Index = i; CatAnh(image, images, i, numRow, numCol, x, y); picBoxes[i].Location = new Point(x * (i % numCol), y * (i / numCol)); if (!pbPuzzle.Controls.Contains(picBoxes[i])) { pbPuzzle.Controls.Add(picBoxes[i]); } } TronAnh(ref indice); for (int i = 0; i < currentLevel; i++) { picBoxes[i].Image = images[indice[i]]; ((MyPB)picBoxes[i]).ImageIndex = indice[i]; } }
public void xephinhClick(object sender, EventArgs e) { ((MyPB)sender).BorderStyle = BorderStyle.FixedSingle; if (firstBox == null) { firstBox = (MyPB)sender; firstBox.BorderStyle = BorderStyle.FixedSingle; } else if (secondBox == null) { secondBox = (MyPB)sender; firstBox.BorderStyle = BorderStyle.Fixed3D; secondBox.BorderStyle = BorderStyle.FixedSingle; Switch(firstBox, secondBox); firstBox = null; secondBox = null; } if (isTrue()) { MessageBox.Show("Hoan thanh "); } }