using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MatchnigGame { public partial class Form1 : Form { Label firstClicked = null; Label secondClicked = null; Random random = new Random(); List icons = new List() { "!", "!", "N", "N", ",", ",", "k", "k", "b", "b", "v", "v", "w", "w", "z", "z" }; private void AssignIconsToSquares() { foreach (Control control in tableLayoutPanel1.Controls) { Label iconLabel = control as Label; if (iconLabel != null) { int randomNumber = random.Next(icons.Count); iconLabel.ForeColor = iconLabel.BackColor; iconLabel.Text = icons[randomNumber]; icons.RemoveAt(randomNumber); } } } public Form1() { InitializeComponent(); AssignIconsToSquares(); } private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e) { } /// /// Every label's Click event is handled by this event handler /// /// The label that was clicked /// private void label1_Click(object sender, EventArgs e) { // The timer is only on after two non-matching // icons have been shown to the player, // so ignore any clicks if the timer is running if (timer1.Enabled == true) return; Label clickedLabel = sender as Label; if (clickedLabel != null) { if (clickedLabel.ForeColor == Color.Black) return; if (firstClicked == null) { firstClicked = clickedLabel; firstClicked.ForeColor = Color.Black; return; } secondClicked = clickedLabel; secondClicked.ForeColor = Color.Black; timer1.Start(); } } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); firstClicked.ForeColor = firstClicked.BackColor; secondClicked.ForeColor = secondClicked.BackColor; firstClicked = null; secondClicked = null; } } }