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.

Make r and target fields of your form.
Perhaps you could have a button New Number with the lines 3 and 4 of your last code in it? Of course remove lines 7 and 8 from your buttonEnter handler.

No, you have to put the instance of the Random on the class level, then you will not have problems with getting random number every where (on load event, or on some button click event, or in some method):

//form1 class:
Random r = new Random();
public Form1()
{
    //constructor of form1
    IntializeComponent();
    int a = r.Next(100,201);  //for example!
    MessageBox.Show(a.ToString());
}

private void  button1_Click()
{
   int myNumber = r.Next(0,101); //for example!
   MessageBox.Show(myNumber.ToString());
}

Hope it helps,
Mitja

mitja,it didn't work.
as int target = r.Next(0,1000 is still under button click.

ddanbe, i do not understand what does it mean?

I mean this:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // 2 "global" fields of the form class
        private Random r = new Random();
        private int target = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Enter_Click(object sender, EventArgs e)
        {
            target = r.Next(0, 101); // gives number from 0 to 100
            //more code

        }
    }
}

nope, bcz. target = r.Next(0, 101); is still under button click.

nope, bcz. target = r.Next(0, 101); is still under button click.

I did this and worked...
thanks.

public partial class Form1 : Form
    {
        private Random r = new Random();
        private int target = 0;
        int counter = 0;
 
        public Form1()
        {
            InitializeComponent();
            target = r.Next(0, 100);
        }

Hi i have a prob i need to do form load if the user the 3 combination of colors can you give some solutions pls

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.