adding of values from a multiline box

how do i add up all my values in a multiline textbox???
MrasherAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I guess in your multiline textbox contains the numeric values?

You can first split the value with carriage return and store the values into an array, and then doing a loop to sum up all the values.

you may also provide us a sample of value in your multiline textbox so that we can provide suggestions accordingly.

And.. do you want to do it in C# or VB Classic? Both approaches could be different.
MrasherAuthor Commented:
the values are taken from my previous calculation.
MrasherAuthor Commented:
as im new to programming. how the code look like. and idone it in c#
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
as mentioned:

1. do you want to do it in C# or VB Classic?
2. can you provide us a sample of the value?
MrasherAuthor Commented:
c# and here is my code
public partial class frmMain : Form
    {
       
        public frmMain()
        {
            InitializeComponent();
            
        }
       
        private void frmMain_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'data1DataSet.patch_cord' table. You can move, or remove it, as needed.
            this.patch_cordTableAdapter.Fill(this.data1DataSet.patch_cord);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.Text;
            textBox2.Text = comboBox1.SelectedValue.ToString();

        }
        
       
        private void button1_Click(object sender, EventArgs e)
        {

            Int32 val1 = Convert.ToInt32(textBox2.Text);
            Int32 val2 = Convert.ToInt32(textBox3.Text);
            Int32 val3 = val1 * val2;
            String val4 =  Convert.ToString(val3);

            textBox4.Text += val4.ToString() + Environment.NewLine;
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            textBox5.Text += textBox4.Text;
            }
        }


    }

Open in new window

sample
GrahamSkanRetiredCommented:
Here is the VB Classic version:
    Dim strText() As String
    Dim i As Integer
    Dim sngTotal As Single
    
    strText() = Split(Text1.Text, vbCrLf)
    For i = 0 To UBound(strText())
        sngTotal = sngTotal + Val(strText(i))
    Next i
    MsgBox "Total is " & sngTotal

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try something like:

String t = yourTextBox.Text;
long total = 0;
String[] arr = t.Split('\n');
for (int i = 0; i < arr.Length; i++)
{
     total += Convert.ToInt32(arr[i]);
}
Console.WriteLine("Total = " + total.ToString());

Open in new window

MrasherAuthor Commented:
erm i cant use something like Console.WriteLine("Total = " + total.ToString()); because im using windows forms
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
so, you can replace that line to something like:
yourAnotherTextBox.Text = total.ToString();

Open in new window

if you would like to output the total to yourTextBox control.
MrasherAuthor Commented:
it show this errorerror
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you can add a try ... catch to handle that as I suspect there are some empty lines or invalid chars which cannot be parse as a number:
try {
         total += Convert.ToInt32(arr[i]);
     } catch (Exception e) {}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrasherAuthor Commented:
it works thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.