I have designed Random number guessing game in windows form application.
after user enters guessed number,hits button.
for button click i entered code:
private void buttonEnter_Click(object sender, EventArgs e)
{
string invalue;
int number;
Random r = new Random();
int target = r.Next(0, 100);
invalue = textBox1.Text;
counter++;
label4.Text = Convert.ToString(counter);
number = int.Parse(invalue);
if (number < target)
{ this.BackColor = Color.LightSkyBlue; }
else if (number > target)
{this.BackColor = Color.BurlyWood;}
else {if (number== target)
label2.Visible = true;
label3.Visible = true;}
}
now problem is that, i get random number on every button click(i guess so.)
I guess bcz i put it under button click.
where can i put it?
i tried to put it under form load event like (cut from button click)
private void Form1_Load(object sender, EventArgs e)
{
Random r = new Random();
int target = r.Next(0, 100);
}
but in that case i get error that target is not defined.
i want to count number of times user guessed.Also, change the color of form if guessed number is less or greater.
I am new to programing. so please explain the procedure.