Click here to Skip to main content
12,070,764 members (63,474 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 16-Jan-12 22:22pm
Edited 22-Dec-15 21:34pm
VICK10.3K
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: 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 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: 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 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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 23 Dec 2015
Copyright © CodeProject, 1999-2016
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