Click here to Skip to main content
14,357,858 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi i am working on c# .net window application

i am unable 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
Updated 18-Oct-19 3:40am
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.
Rate this:
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();
    }
   
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:
Rate this:
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.
   
Rate this:
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.
   
Rate this:
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.
   
Comments
[no name] 18-Oct-19 8:40am
   
*spam redacted*
Rate this:
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();
   
v2

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




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