Introduction
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
- Open Visual Studio-->File Menu --->New project.
- Choose Windows Form application.
- 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.
- Maximize Box is set to False.
- Start Location may be central or Windows default location as required.
Step 3 - Control Functions
- Drag and Drop buttons, labels and text boxes from the toolbar.
- A button accepts clicks. In this project five buttons are used, that accept click events and perform actions in the user interface.
- In this project, three labels are used to display text on a form.
- 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.
- private void button2_Click(object sender, EventArgs e)
- {
- int num= int.Parse(textBox1.Text);
- int num2 = int.Parse(textBox2.Text);
- int sum = num + num2;
- textBox3.Text = sum.ToString();
-
- }
SUB Button Click.
- private void button3_Click(object sender, EventArgs e)
- {
- int num = int.Parse(textBox1.Text);
- int num2 = int.Parse(textBox2.Text);
- int sum = num - num2;
- textBox3.Text = sum.ToString();
- }
MUL Button Click.
- private void button4_Click(object sender, EventArgs e)
- {
- int num = int.Parse(textBox1.Text);
- int num2 = int.Parse(textBox2.Text);
- int sum = num * num2;
- textBox3.Text = sum.ToString();
- }
DIV Button Click.
- private void button6_Click(object sender, EventArgs e)
- {
- int num = int.Parse(textBox1.Text);
- int num2 = int.Parse(textBox2.Text);
- int sum = num % num2;
- textBox3.Text = sum.ToString();
- }
EXIT Button Click.
- private void button1_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
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
Summary
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.