Click here to Skip to main content
15,912,207 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
I have to write the code on button click for addition of two nos.

Can anyone tell why the code given below is not working?
C#
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 myproject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int number1;
            int number2;
            int answer;



number1 = 3;
number2 = 5;

answer = number1 + number2;

//MsgBox(answer);


        }
    }
Posted
Updated 7-Mar-11 18:51pm
v2
Comments
Raj.rcr 8-Mar-11 1:11am    
You can simply use either a message box or textbox to display the result..

That code seems quite ok. To see the results you can write:
MessageBox.Show(answer.ToString(); //Shows a message box with the content "8"

If this is not the problem, you should show us the error message.
 
Share this answer
 
v2
Comments
thatraja 8-Mar-11 0:57am    
Hey, where were you? can't saw you here on last week.
:)
Member 7673456 8-Mar-11 1:29am    
Nothng Was lill Busy....
Espen Harlinn 13-Mar-11 8:56am    
Right - even simpler, my 5 :)
Add a textbox to display the result..

then,

TextBox1.Text = answer.Tostring();
 
Share this answer
 
v2
Comments
Espen Harlinn 13-Mar-11 8:55am    
Nice and simple - a 5

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