A Calculator via Visual C#

Preface

The .NET Framework provides a long list of Math APIs for calculation purposes. Our goal now is to build a calculator using some basic math operations and some of those APIs. Normally when working with Windows Forms we use a button to fire an event that warrants an event handler. But how do use the Button control to enter numeric character input into a TextBox control, and then further use other buttons to perform operations needed on the entered input? First off, there are 10, or 0 – 9 digits, that must have their text properties set to those integers to appear on the buttons. The other buttons need to have their text properties set to the operation in which they are to perform when the user clicks them. With all of those buttons, we should group them. This means that we need to use two GroupBox controls to contain the input field and the Button controls. We then need to declare some local variables, particularly to indicate the status of an operation. Sounds like a Boolean data type. We then need to write a method that calculates the total of the particular operation performed. Because there will be several different types of cases at hand, we’ll need to use the switch statement.

Now if choose to get the square root of 65,536 (2 to the 16th power), we can just press a button:

I used this example because there are two possible values for a data bit: 1 or a 0. There are 8 bits in a byte. 2 raised to the 8th power = 256, which is 16 squared. 65,536 is equal to one segment of memory. Anyway, here is the code for the calculator. Try and expand on it by adding some trigonometric functions. That is the purpose of this article. This code is not split into partial classes. The downloadable file is, however. Unzip it the solution container into a newly made folder in your Projects directory, and then double-click the solution file.

Share

About the Author

I started electronics training at age 33. I began studying microprocessor technology in an RF communications oriented program. I am 43 years old now. I have studied C code, opcode (mainly x86 and AT+T) for around 3 years in order to learn how to recognize viral code and the use of procedural languages. I am currently learning C# and the other virtual runtime system languages. I guess I started with the egg rather than the chicken. My past work would indicate that my primary strength is in applied mathematics.

Comments and Discussions

Writing a calculator in C# is equal to writing a "Hello World" program. When you learn to code, submit an article that is more advanced. For the noobs there are many tutorials much more better than your so called "article"

PS:I noticed that you have not even renamed you buttons. They are called as default: button1, button2, etc. You have to learn a lot more and to gain much more experience in coding before diving into articles writing...