samedi 6 mai 2017

c# Windows form application, wrong return type

I'm trying to learn how to create Windows form applications in Visual studio. My first little mini project is to create a simple Numbers application, which gives the user two random numbers from 0-100, and adds these two together.

    private int button1_Click(object sender, EventArgs e)
    {
        int rn1 = Random();                
        textBox1.Text = rn1.ToString();

        return rn1;
    }

    private int button2_Click(object sender, EventArgs e)
    {
        int rn2 = Random();
        textBox2.Text = rn2.ToString();

        return rn2;

    }

    private void button3_Click(object sender, EventArgs e)
    {
        //Add the two numbers together
    }

    public int Random()
    {
        Random r = new Random();
        int rn = r.Next(0, 100);
        return rn;
    }

Here is the code snippit i'm working on. "button1_click" and "button2_click" both adds a random number in a textbox in my application. The third button "button3_click" is where ill be adding the two numbers together like so:

    private void button3_Click(object sender, EventArgs e)
    {
        int number1 = button1_Click;
        int number2 = button2_Click;

        int sum = number1 + number2;
        textBox3.Text = sum.ToString();
    }

I want the first two methods to return the number that the Random() method creates, but I get the error "'int Form1.button_click(object, EventArgs)' has the wrong return type", and the same with button2.

Hope someone can help :)




Aucun commentaire:

Enregistrer un commentaire