private void Button_MouseDown(object sender, MouseEventArgs e) { if (hasWon) { return; } // Get the button object. Button button = (Button)sender; Tile tile = default; // Find the KeyValuePair that corresponds to our button in tiles. foreach (KeyValuePair <Button, Tile> btn in tiles) { // If the KeyValuePair we're checking against has our button, then we found it. if (btn.Key.Equals(button)) { // Get the tile object that corresponds to our Button. tile = btn.Value; } // Add the button to the form. Controls.Add(button); } // If we clicked mouse1. if (e.Button == MouseButtons.Left) { if (tile.isMine) { foreach (KeyValuePair <Button, Tile> btn in tiles) { if (btn.Value.isMine) { btn.Key.Image = null; btn.Key.BackColor = Color.Red; } } bombDetonate.Play(); // Tell the user that they lost. MessageBox.Show("You lost.", "Minesweeper", MessageBoxButtons.OK); // Reset the play area. DisposeTiles(); SetupTiles(); // Reset the clock. seconds = 0; minutes = 0; } else { tile.Click(); int mineCount = GetTileNumber(tile); if (mineCount > 0) { tile.button.Text = mineCount.ToString(); } button.BackColor = Color.Green; if (GameOptions.numberOfClicks == GameOptions.nonBombCount) { Leaderboard leaderboard = new Leaderboard(); leaderboard.GetLeaderboard(); string time = LabelTimer.Text; hasWon = true; MessageBox.Show("You Won!", "Minesweeper", MessageBoxButtons.OK, MessageBoxIcon.Information); (bool, int)qualify = leaderboard.CheckIfQualifies(time); KeyValuePair <string, string> userInformation = new KeyValuePair <string, string>(GameOptions.username, time); if (qualify.Item1) { leaderboard.InsertItem(qualify.Item2, userInformation); } } } } else if (e.Button == MouseButtons.Right && !tile.HasBeenClicked && flags < flagCap) { tile.HasBeenFlagged = !tile.HasBeenFlagged; if (tile.HasBeenFlagged) { button.Image = flagImage; flags++; } else { button.Image = null; flags--; } // If your remaining flags is less than 10, then add a 0 as the first digit to format it like this: 01, 02, 03 etc... LabelFlags.Text = $"Flags: {(flagCap - flags < 10 ? "0" : "")}{flagCap - flags}"; } }