Performing Arithmetic Operation of Two Variable Using Windows Form Application In Visual Studio 2017


Windows.Forms are used to create powerful Windows-based applications. In this article, I will explain how to perform Arithmetic operations using Windows.Forms
Step 1 - Start the Project
  1. Open Visual Studio-->File Menu --->New project.
  2. Choose Windows Form application.
  3. Start project as Windows Forms app 9.
Step 2 - Designer pages and their properties

Now, the corresponding designer page will be loaded and the following changes are done in the properties of the form application for performing arithmetic operations.
  1. Maximize Box is set to False.
  2. Start Location may be central or Windows default location as required.
Step 3 - Control Functions
  1.  Drag and Drop buttons, labels and text boxes from the toolbar.
  2.  A button accepts clicks. In this project five buttons are used, that accept click events and perform actions in the user interface.
  3.  In this project, three labels are used to display text on a form.
  4.  Three text boxes are used - two of which are used to get user input and the remaining one is for displaying the result.
Step 4 - Coding for Button Click

ADD Button Click.
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     int num= int.Parse(textBox1.Text);  
  4.     int num2 = int.Parse(textBox2.Text);  
  5.     int sum = num + num2;  
  6.     textBox3.Text = sum.ToString();  
SUB Button Click.
  1. private void button3_Click(object sender, EventArgs e)  
  2.         {  
  3.             int num = int.Parse(textBox1.Text);  
  4.             int num2 = int.Parse(textBox2.Text);  
  5.             int sum = num - num2;  
  6.             textBox3.Text = sum.ToString();  
  7.         }  
MUL Button Click.
  1. private void button4_Click(object sender, EventArgs e)  
  2.       {  
  3.           int num = int.Parse(textBox1.Text);  
  4.           int num2 = int.Parse(textBox2.Text);  
  5.           int sum = num * num2;  
  6.           textBox3.Text = sum.ToString();  
  7.       } 
DIV Button Click.
  1. private void button6_Click(object sender, EventArgs e)  
  2.         {  
  3.             int num = int.Parse(textBox1.Text);  
  4.             int num2 = int.Parse(textBox2.Text);  
  5.             int sum = num % num2;  
  6.             textBox3.Text = sum.ToString();  
  7.         }  
EXIT Button Click.
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     Application.Exit();  
  4. }  
Here, we use the parse in coding for converting the string to a 32-bit integer. 

Step 5 - Compile and Run

Now, let's debug. The output is obtained for each operation by clicking the corresponding buttons.

First, enter the value in textbox1 and textbox2. The result will be obtained in textbox3 when the corresponding button is clicked.


Sample output obtained for the division of two numbers.

A=3 entered in textbox1
B=3 entered in textbox2
Output=0 obtained in textbox3
Sample output is obtained for the multiplication of two numbers.

A=3 entered in textbox1
B=3 entered in textbox2
Output=9 obtained in textbox3

This article is for absolute beginners to Visual Studio 2017. In my next article, I will give an idea of how to create the login form using the same Windows.Forms application.

Similar Articles