namespace HangMan { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
string[] words = { "PENCIL", "UMBRELLA", "RUBBER", "RING", "NECKLACE", "SPEED", "PROGRAMMER" };
private void Form1_Load(object sender, EventArgs e)
{
setWords();
getWords();
}
private void getWords()
{
Random rnd = new Random();
string selectedWord = words[rnd.Next(0, words.Length)];
foreach (char item in selectedWord)
{
Label lbl = new Label();
lbl.AutoSize = false;
lbl.Width = 25;
lbl.Height = 25;
lbl.Text = "_";
flowLayoutPanel2.Controls.Add(lbl);
}
}
private void setWords()
{
for (char i = 'A'; i <= 'Z'; i++)
{
Button btn = new Button();
btn.Text = i.ToString();
btn.Width = 25;
btn.Height = 25;
btn.Click += Btn_Click;
flowLayoutPanel1.Controls.Add(btn);
}
}
int Score = 0;
private void Btn_Click(object sender, EventArgs e)
{
Button clicked = sender as Button;
bool IsCharOpened = false;
foreach (Control item in flowLayoutPanel2.Controls)
{
if (item is Label)
{
Label lbl = (Label)item;
if (lbl.Tag.ToString() == clicked.Text)
{
lbl.Text = clicked.Text;
Score += 100;
IsCharOpened = true;
}
}
}
if (IsCharOpened == false)
{
lblRemaining.Text = (int.Parse(lblRemaining.Text) - 1).ToString();
if (int.Parse(lblRemaining.Text) == 0)
{
MessageBox.Show("YOU HAVE FAILED..!");
Application.Exit();
}
}
lblScore.Text = Score.ToString();
clicked.Enabled = false;
}
}
}
I cannot find any solution, and I am new on c#. I need your help guys, thanks.