Nowadays, most calculators are powered by electricity. Engineers and students prefer battery-operated calculators. Before 1970, the slide rule, a cruder calculator, was widely used. It consisted of a slide, a wooden slat that could be slid between two reinforced slats. The slide and the outside slats had calibrated numerical scales.

#### Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTME

## Project Creation for Calculator in C# In Visual Studio

You will use visual studio 2022 for this project. You will follow the following steps to create a project:

• You must click on “Create a New Project.”

• Then you must search “Windows Forms App (.NET Framework).”

• Then you must give this project a name like “calculator demo.”

Now, work on the GUI of the calculator.

#### New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & More

## GUI Creation of Calculator in C#

• You must change the window label from form1 to the “Basic Calculator.”
• Then you should start by adding some labels to understand the GUI.
• You can find the label in the toolbox. To access the toolbox go to view and select toolbox.
• The first label is a calculator in size 20, blue.
• Then add three more labels for value 1, value 2, and the output.
• You should add some text boxes to take input and show output according to the arithmetic operations you will be performing.
• You must add some buttons to do operations like addition, subtraction, multiplication, division, and percentage.

Now, you will write a code for the arithmetic function.

Also Read: 4 Free and Low-Cost Online Resources for Practicing Code

## Button Click Functionality Code for Calculator in C#

You must write code for each button separately.

### 1. You Must Start With the Addition (+) Button:

private void button1_Click(object sender, EventArgs e)

{

/* we will take the textbox’s value for value 1 and 2 as double and store it in txt1 and txt2 */

double txt1 = Convert.ToDouble(textBox1.Text);

double txt2 = Convert.ToDouble(textBox2.Text);

double sum = txt1 + txt2;

//Now to display the sum in textbox3.

//But we will convert this value to string

//because textbox only recognizes strings.

textBox3.Text = sum.ToString();

}

### 2. You Should Write the Code for the Subtraction (-) Button:

private void button2_Click(object sender, EventArgs e)

{

double txt1 = Convert.ToDouble(textBox1.Text);

double txt2 = Convert.ToDouble(textBox2.Text);

double diff = txt1 - txt2;

textBox3.Text = diff.ToString();

}

#### Full Stack Web Developer Course

To become an expert in MEAN Stack

### 3. You Must Write Up the Code for the Division (/) Button:

private void button3_Click(object sender, EventArgs e)

{

double txt1 = Convert.ToDouble(textBox1.Text);

double txt2 = Convert.ToDouble(textBox2.Text);

double division = txt1 / txt2;

textBox3.Text = division.ToString();

}

### 4. Write the Code for the Multiplication (*) Button:

private void button4_Click(object sender, EventArgs e)

{

double txt1 = Convert.ToDouble(textBox1.Text);

double txt2 = Convert.ToDouble(textBox2.Text);

double product = txt1 * txt2;

textBox3.Text = product.ToString();

}

### 5. You Should Write the Code for the Percentage (%) Button:

private void button5_Click(object sender, EventArgs e)

{

double txt1 = Convert.ToDouble(textBox1.Text);

double txt2 = Convert.ToDouble(textBox2.Text);

double percent = (txt1 / txt2)*100;

textBox3.Text = percent.ToString();

}

## Next Steps

"C# Arraylists" can be your next concept to learn. ArrayList is an ordered collection of objects that can be individually indexed. It is essentially an array substitute. It also supports dynamic memory allocation and adds searches and sorts items in a list.

You've come to the right place if you want to learn more about software development and possibly work in it. Caltech CTME and IIT-Kanpur collaborate in teaching our Software Development Courses. These classes cover data structures and algorithms from fundamentals to advanced concepts like Competitive Programming. You'll learn about data structures like trees, graphs, and queues as a software developer.

If you have any queries about this "Calculator in C#" tutorial, please drop them in the comment section below. Our 24/7 expert team will promptly respond to your queries.

## About the Author

Kartik Menon

Kartik is an experienced content strategist and an accomplished technology marketing specialist passionate about designing engaging user experiences with integrated marketing and communication solutions.

View More
• Disclaimer
• PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.