Click here to Skip to main content
16,007,843 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my question is that what is the code to add two numbers using button?

What I have tried:

code is here of class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
class Class1
{
int a;
int b;
int sum;
public void main()
{
a = 5;
b = 6;
sum= a + b;
MessageBox.Show("the sum is="+sum.ToString());

}
}
}
and i need button code please tell me button code..?
Posted
Updated 27-Aug-16 18:31pm

Read these docs first
Windows Form - TextBox Control[^]
Windows Form - Button Control[^]

C#
private void btnAdd_Click(object sender, EventArgs e)
      {
          /*
           * add two controls in the form and rename the 'name' property as txtA and txtB
           * add a button control and rename it to btnAdd, then double click the button, visual studio IDE wll add this event code for you
           * and you add your logic in this code block as below.
           */
          int a = Convert.ToInt32(txtA.Text.Trim()); // convert to int .. explore int.TryParse for validation
          int b = Convert.ToInt32(txtB.Text.Trim()); // convert to int .. explore int.TryParse for validation
          int sum = a + b;
          MessageBox.Show("the sum is=" + sum.ToString());

      }
 
Share this answer
 
There is no windows form class. You should create new Windows Forms Application. You see a form. Visual studio have left side Toolbox, look at there. Toolbox contains textbox, button etc. You have to drag and drop two textbox and a button. And double click the button. Now you view code behind. You use F7 key for switch to designer/coder screen.
 
Share this answer
 

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