In: Computer Science
Problem 6.3 Sum of Numbers - Similar to Problems 6.1 & 6.2, you must sum up 5 numbers. However, you will obtain these numbers from a list box for which you have used the String Collection Editor to insert a number on each of 5 lines in the box. You may use any type of loop. The result is displayed in a text box. The program is initiated with "Go" button.
Problem 6.3 does NOT require an Input Dialog Box. Instead, YOU
the PROGRAMMER put a list box control on the form. The PROGRAMMER
loads up the list box at design time with all five (5) numbers to
be summed.
Step1: Create a C# WindowsFormApplication and set a name of your choice.
Step2: Design the form with ListBox, Button and TextBox as shown in the below screenshot

Step3:
Set the following properties of the controls on the form

Step4:
Double click the Go button and write the below code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SumupCollection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           
InitializeComponent();
        }
        private void
btnGo_Click(object sender, EventArgs e)
        {
           
//Create an array of string objects
           
String[] nums = new String[5];
           
nums[0] = "1";
           
nums[1] = "2";
           
nums[2] = "3";
           
nums[3] = "4";
           
nums[4] = "5";
           
//Add listbox elements to the listbox1
           
listBox1.Items.AddRange(nums);
           
double total = 0;
           
//use foreach loop to find the total of the itesm in the
listBox1
           
foreach (String item in listBox1.Items)
           
{
               
total = total + int.Parse(item);
           
}
           
//Show the total value in result text box
           
resultTxtBox.Text = total.ToString();
        }
    }
}
Step5:
Run the application :Output
