开发者

How do I code an event that calls a method

开发者 https://www.devze.com 2023-02-05 20:57 出处:网络
OK, I have 5 methods Move(), TurnRight(), TurnLeft(), Accelerate(), Decelerate() I have 5 buttons on my windows form called:

OK,

I have 5 methods

Move(), TurnRight(), TurnLeft(), Accelerate(), Decelerate()

I have 5 buttons on my windows form called:

Move, Turn Right, Turn Left, Accelerate, Decelerate

How do take values entered from texboxes and store them, so that when a user clicks one of those buttons it calls the method to perform calculations, and then display the information.

I just don't know how or what to code inside the event that I activated from the form. Can anyone show me a sample of how they would do it?

Here is my move method

public void move() { double radians = direction * Math.PI / 180;

        //change the x location by the x vector of the speed
        X_Coordinate += (int)(speed * Math.Cos(radians));

        //change the y location by the y vectior of the speed
        Y_Coordinate -= (int)(speed * Math.Sin开发者_开发百科(radians));


    }

I created a button on my form and named it btnMove

What should I code inside the event for my move button to call the move method and the display that for an outcome?


Say you want to calculate the info in two text boxes, and show the result in the third, you cold do this.

private void Button1Click(object sender, EventArgs e)
{
    var x = int.Parse(textBox1.Text);
    var y = int.Parse(textBox2.Text);

    textBox3.Text = (x + y).ToString();
}

say you shold pass the value over to the move method, you cold do this change the textBox3.Text = (x + y).ToString(); with move(x + y); if the move takes in an int like this

public void move(int vaule)
{
   //do stuff with value
}


For each buttons click event (Double click on button in designer to open code for click event) call your respective method.

However its not clear if your Move()...etc methods take Values.

You can try this

public void btnMove_Click(object sender, EventArgs e)
{
     Move();
}

public void Move()
{        
     string thetext = Convert.ToInt32(this.textbox1.Text);
     //Perform Calculations
}

similarly for other buttons


I would store the initial values of the textboxes outside the method in the initiation of the class. Then in the button click event update the values that have changed. E.g.

int val1 = parseInt(textbox1.text);

int val2 = parseInt(textbox2.text);

private void Move()

{

 val2 = parseInt(textbox2.text);

}

0

精彩评论

暂无评论...
验证码 换一张
取 消