开发者

Simple question from a very no experienced programmer

Simple question: This is a method I created

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))开发者_如何学C;

    //change the y location by the y vectior of the speed
    Y_Coordinate -= (int)(speed * Math.Sin(radians));
}

How do I take a value enter into a text box, use a button called move, to call this method and display the result?


Check out the video tutorial from ASP.Net at http://www.asp.net/general/videos/intro-to-aspnet-controls on an introduction on asp.net controls. It will help you define a button with an event.


If you're using Visual Studio then...

Open the designer, and, from the toolbox, drag a TextBox and a Button onto the page (along with any other TextBox items you need, I'll assume you have them from now on. Double click the button you just placed on the page. This creates an event handler, code within the scope of this block will execute when the button has been clicked. Place the following code within the generated event handler code block, changing appropriately (e.g. TextBox names (You can change these using the Property Explorer within VS)):

var direction = 0;
if (int.TryParse(DirectionTextBox.Text, out direction))
{
    //further validate input here, as necessary

    Move(direction);
    XTextBox.Text = X_Coordinate.ToString();
    YTextBox.Text = Y_Coordinate.ToString();
}
else
{
    //handle invalid input, if not already done elsewhere.
}

Presumably here X_Coordinate and Y_Coordinate are accessible variables. So, now, alter your move method to accept a parameter, like so:

public void Move(int direction)
{
    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));
}

Note that by this point I have no idea which of the elements of the Move method you wish to be variable input, but I'll keep thinking along the lines of this being an idea to can extend to as many elements as you like/need. Also note that there are many other ways to go about this - what I suggest is neat enough (given the information we have) and not too far altered from your initially posted code, hence, hopefully, easier for you to implement; but you could return a composite type from this method, and set visual outputs with the directly returned result to make things more concise.


Hook up the Click event of the button, and in the event handler you can access the entered value by TextBoxName.Text. You will probably need to parse the string value using int.Parse in order to use it in a calculation.

Your button will look something like this.

<asp:Button ID="MoveBtn" runat="server" onclick="Move" Text="Move" />

And then in your code behind

protected void Move(object sender, EventArgs e)
{
   // Call your move method here.
   // If it is in the same class, it's just move();
   // Otherwise you need a reference to your class so you can call it.
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜