Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.40/5 (9 votes)
See more:
Hi i am working on c# .net window application

i am unable to add two numbers

C#
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
Updated 22-Jul-20 5:32am
v3
Comments
Emre Ataseven 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.

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:
C#
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();
    }
 
Share this answer
 
Comments
Reiss 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 17-Jan-12 4:54am    
True - but as he is a beginner and mentioned ints I just went with the theme! :laugh:
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.
 
Share this answer
 
There is a control called NumericUpDown for Windows forms. Have a look at that. It will save you from overhead like conversion and validations.
 
Share this answer
 
If I modify you code, this should work:
C#
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.
 
Share this answer
 
Comments
[no name] 18-Oct-19 8:40am    
*spam redacted*
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

C#
int c = a + b;

Ans.Text = c.ToString();
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900