简体   繁体   中英

How to accept values into array using Textbox and display the Sum of numbers using a Button?

I have three TextBoxes and a Button. I should be able to enter multiple numbers on the TextBoxes. When I click on the Button, the sum of the numbers should be displayed on another TextBox. How to accept values into array using Textbox and display the Sum of numbers using a Button?

You could do something like the following:

 int[] numbers = new int[2];  // Declare the array 

 public void Button1_Click() {
    numbers[0] = Int32.Parse(TextBox1.Text);
    numbers[1] = Int32.Parse(TextBox2.Text);
    numbers[2] = Int32.Parse(TextBox3.Text);

    TextBox4.Text = numbers.Sum().ToString();
 }

Note: You probably want to add some error handling into the application to ensure that the textbox values are indeed numbers. Also use TryParse rather than the standard Parse .

您可以使用一些单独的例如逗号,然后将您的Textbox值拆分为获取字符串数组,然后您可以尝试将字符串解析为int或decimal。然后计算总和

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM