im kinda little noob in c# so i need you help.
hello guys im having a problem on my textbox, i have a textbox and it is multi line, what i want is sum all the numbers on my textbox
and display it to txtboxSum
for example,
textbox
4
5
2
1
4
sum
but i dont know how to do this, i dont have any idea. i hope that you can help me guys thanks
Loading

Hemant KumarPosted Sep 21, 2011, 8:24 AM
String text = txtbx.Text;
String[] parts = text.Split(new Char[] { ',', ':', ';', '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
int temp = 0;
foreach (String item in parts)
{
temp += int.Parse(item);
}
Vilas GitePosted Aug 6, 2011, 6:50 AM
Vulpes Sir's code very good.
@ Gabriel, For more details work out on database…
http://www.c-sharpcorner.com/UploadFile/73ba73/6683/
gabriel yapPosted Aug 6, 2011, 4:34 AM
and its happens that what it sum is only a single digit
1+5+0+0+0
4+0+0
7+0+0+0
now i know where dit it get the 17 :)
btw sir thx, my question has been answered..
Krishna GaradPosted Aug 5, 2011, 5:01 AM
1
2
3
like this not for 10 11 12
use vulpes provided code it will work.
VulpesPosted Aug 5, 2011, 4:07 AM
int sum = 0;
int number;
foreach (string line in textBox1.Lines)
{
int.TryParse(line, out number);
sum += number;
}
MessageBox.Show("sum is " + sum);
gabriel yapPosted Aug 5, 2011, 3:53 AM
the numbers on my textbox is this..
15000
400
7000
but the sum it display to my sum txtSum the answer is
17
lookes like its fails on sum
Krishna GaradPosted Aug 5, 2011, 12:51 AM
List
string _no = txtno.Text;
int _sum=0;
for (int i = 0; i < _no.Length; i++)
{
if (_no[i]>=49&&_no[i]<=57)//Check on ascii
{
lst.Add(int.Parse(_no[i].ToString()));
}
}
foreach (int item in lst)
{
_sum += item;
}
txtsum.Text = _sum.ToString();
Krishna GaradPosted Aug 5, 2011, 12:35 AM
4
5
6
8
or
4 5 6 8
means new line or space is coming in no.Then try this
List
string _no = txtno.Text;
int _sum=0;
for (int i = 0; i < _no.Length; i++)
{
if (_no[i].ToString() == "\n" || _no[i].ToString() == "\r"||_no[i].ToString()==" ")
{
//Do Nothing
}
else
{
lst.Add(int.Parse(_no[i].ToString()));
}
}
foreach (int item in lst)
{
_sum += item;
}
txtsum.Text = _sum.ToString();