adding of values from a multiline box

how do i add up all my values in a multiline textbox???
MrasherAsked:
Who is Participating?
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 ChongCommented:
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.
0
MrasherAuthor Commented:
the values are taken from my previous calculation.
0
MrasherAuthor Commented:
as im new to programming. how the code look like. and idone it in c#
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ryan ChongCommented:
as mentioned:

1. do you want to do it in C# or VB Classic?
2. can you provide us a sample of the value?
0
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
0
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

0
Ryan ChongCommented:
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

0
MrasherAuthor Commented:
erm i cant use something like Console.WriteLine("Total = " + total.ToString()); because im using windows forms
0
Ryan ChongCommented:
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.
0
MrasherAuthor Commented:
it show this errorerror
0
Ryan ChongCommented:
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

0

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!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.