Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi i am working on c# .net window application
 
i cont able to add two numbers
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace Add
{
    public partial class calci : Form
    {
        public calci()
        {
            InitializeComponent();
        }
 
        private void But_Add_Click(object sender, EventArgs e)
        {
          int a = Convert.ToInt32(Txt_1.Text );
          int b = Convert.ToInt32(Txt_2.Text);
 
            Ans.Text = a + b;
           
 
        }
 
    }
}
 
[edit]Code block added - OriginalGriff[/edit]
Posted 16-Jan-12 22:22pm
Edited 16-Jan-12 22:26pm
v2
Comments
Emre Ataseven at 23-Apr-14 16:16pm
   
You need to read a C# for beginners book before starting to write code, blind coding is not a good way to improve yourself.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It's all going to depend on what your user types into the text boxes. If he types some invalid characters, then that code will not work. You also need to convert your number to a string in order to display it!
Try this:
        private void butAdd_Click(object sender, EventArgs e)
            {
            int a;
            int b;
            if (!int.TryParse(tbFirstNumber.Text, out a))
                {
                MessageBox.Show("I need just a number in the first box!");
                return;
                }
            if (!int.TryParse(tbSecondNumber.Text, out b))
                {
                MessageBox.Show("I need just a number in the second box!");
                return;
                }
            labAnswer.Text = (a + b).ToString();
            }
  Permalink  
Comments
Reiss at 17-Jan-12 4:50am
   
This works for ints - the OP mentioned numbers although code shows he is trying to do it with ints, so may need to use the Double.TryParse method
OriginalGriff at 17-Jan-12 4:54am
   
True - but as he is a beginner and mentioned ints I just went with the theme! :laugh:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

If I modify you code, this should work:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace Add
{
    public partial class calci : Form
    {
        public calci()
        {
            InitializeComponent();
        }
 
        private void But_Add_Click(object sender, EventArgs e)
        {
          int a = Convert.ToInt32(Txt_1.Text );
          int b = Convert.ToInt32(Txt_2.Text);
          int c = a + b;
          Ans.Text =  c.ToString();
        }
 
    }
}
Only change you need to do is:
1) check if numbers are not input and throw exception
2) convert added numbers to string to assign to textbox. I would like to take third variable 'c' to make this look good.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As you may easily find in the documentation[^], Convert.ToInt32 method may throw exceptions, hence you, as the wise developer should handle in your code such a possibility. See the sample code in the documentation.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is a control called NumericUpDown for Windows forms. Have a look at that. It will save you from overhead like conversion and validations.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

If you are converting the values to integer first then you have to store the output values in integer itself and then assign it to the textbox. that will be fine
 
int c = a + b;
 
Ans.Text = c.ToString();
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

\\I have a problem with this code..Please help me.After taking two integer values..I still get a concate value from both variables.I want to add both the values and get a messagebox with its answer.
 

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 Concat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
           
            int intX = 53;
            int intY = 10;
            
            MessageBox.Show("The sum of " + intX + "+" + intY + "=" + intX + intY);
 
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            string strone = "Visual Studio ";
            string strtwo = "2008";
            MessageBox.Show(strone + strtwo);
        }
    }
}
 
Edit: inserted code in code-block.
Submit a question next time. Wink | ;)
  Permalink  
v2
Comments
Amund Gjersøe at 10-Sep-12 6:44am
   
This is a question; not a solution. When building strings the data values are converted to text using all objects ToString-method. So the solution for you is: MessageBox.Show("The sum of " + intX + "+" + intY + "=" + (intX + intY).ToString());
malik saifullah at 17-Oct-12 13:36pm
   
nice....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int n1, n2;
            Console.WriteLine("Please Enter number 1");
            n1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter number2");
            n2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Sum of numbers is {0}",n1+n2);
            Console.ReadKey();
        }
    }}
 
hope this helps :) ~Elan~
  Permalink  
Comments
CHill60 at 23-Apr-14 17:48pm
   
Apart from the fact the original post is over 2 years old, and apart from the fact it already had several solutions, the OP quite clearly was using a windows application, collecting the data from textboxes, not the console. Off topic.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace add
 
{
    class Program
    {
        static void Main(string[] args)
        {
            int a,b,c;
            Console.WriteLine("Enter the first number");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter second number");
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            Console.WriteLine("The addition of two number is {0}", c);
            Console.ReadLine();
        }
    }
}
  Permalink  
v2
Comments
CHill60 at 2-Jul-14 12:01pm
   
Reason for my vote of 1 - No different to previous solutions. Two and a half years late. Also presented as console application when OP was using WinForms
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Everything is OK in you code except the following
1. Take third variable to store sum value for better understanding.
2. Assign third variable value to label like this labelid.Text=thirdvariable.ToString()
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace Add
{
    public partial class calci : Form
    {
        public calci()
        {
            InitializeComponent();
        }
 
        private void But_Add_Click(object sender, EventArgs e)
        {
          int firstval = Convert.ToInt32(Txt_1.Text );
          int secondval = Convert.ToInt32(Txt_2.Text);
          int Total_Sum= firstval + secondval;
          Ans.Text = Total_Sum.ToString();
 

        }
 
    }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 265
2 Praneet Nadkar 237
3 Marcin Kozub 225
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 3 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100