Reputation: 43
So I want to make a program where I can plus the value in two or more text boxes together. You can see on the picture I want to plus spm1 and spm2 together, and make the value show under >points
Any help is appreciated!
private void btnsum_Click(object sender, EventArgs e) {
Convert.ToInt32(txtspm1.Text.ToString());
Convert.ToInt32(txtspm2.Text.ToString());
sum = txtspm2.Text+txtspm1.Text;
lblsum.Text = sum.ToString();
Upvotes: 2
Views: 122
Reputation: 1088
If you want to find the sum of all the numbers entered in textboxes then you can just give a common class name to all those textboxes and then
on textbox change event write this code in Jquery
var value=$('.txtClass').val();
$('#total').text(value);
Just saw your updated question although usually jquery is used for the given scenario but since you need it on server side so you can simply do this
int val1= Convert.ToInt32(txtspm1.Text);
int val2= Convert.ToInt32(txtspm2.Text);
lblsum.Text = (val1+val2).ToString();
Upvotes: 2
Reputation: 196
sum = txtspm2.Text+txtspm1.Text
.Text returns a string value, so this is concatenating two strings, not adding two integers. Convert the values to integers, then add them.
Upvotes: 4
Reputation: 6720
From
private void btnsum_Click(object sender, EventArgs e) { Convert.ToInt32(txtspm1.Text.ToString()); Convert.ToInt32(txtspm2.Text.ToString()); sum = txtspm2.Text+txtspm1.Text; lblsum.Text = sum.ToString(); i tried this but this just added the nubers like if i put the numbers 2 and 4 then hit the button i just got 42
Your problem is that +
operator behaves different from string and integers, in your case. try this instead
var n1= Convert.ToInt32(txtspm1.Text);
var n2= Convert.ToInt32(txtspm2.Text;
sum =n1+n2;
lblsum.Text = sum.ToString();
+
in string concatenates thats why you get that result
Upvotes: 2
Reputation: 364
private void btnsum_Click(object sender, EventArgs e)
{
var n1= Convert.ToInt32(txtspm1.Text);
var n2= Convert.ToInt32(txtspm2.Text);
lblsum.Text = (n1+n2).ToString();
}
Upvotes: 1