Click here to Skip to main content
11,641,668 members (60,668 online)
Click here to Skip to main content

A Calculator via Visual C

, 13 May 2010 CPOL 12.6K 1 5
Rate this:
Please Sign up or sign in to vote.
An Article the Demonstrates How to Build a Windows Forms Calculator

A Calculator via Visual C#


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.

Examine this code that compiles as a console application:

using System;
public class Program
   public static void Main()
    Console.WriteLine("Abs({0}) = {1}", -55, Math.Abs(-55));
    Console.WriteLine("Ceiling({0}) = {1}", 55.3, Math.Ceiling(55.3));
    Console.WriteLine("Pow({0},{1}) = {2}", 2, 16, Math.Pow(2, 16));
    Console.WriteLine("Round({0},{1}) = {2}",
    10.55358, 2, Math.Round(10.55358, 2));
    Console.WriteLine("Sin({0}) = {1}", 323.333, Math.Sin(323.333));
    Console.WriteLine("Cos({0}) = {1}", 323.333, Math.Cos(323.333));
    Console.WriteLine("Tan({0}) = {1}", 323.333, Math.Tan(323.333));
    Console.WriteLine("Sqrt({0}) = {1}", 65536, Math.Sqrt(65536));

Here we are using the .NET Framework Math APIs directly. It is obviously very simple to type in an API and plug in a value. Here is the result:

Abs(-55) = 55
Ceiling(55.3) = 56
Pow(2,16) = 65536
Round(10.55358,2) = 10.55
Sin(323.333) = 0.248414709883854
Cos(323.333) = -0.968653772982546
Tan(323.333) = -0.256453561440193
Sqrt(65536) = 256

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.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

    public  class Form1 : Form
      private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox txtInput;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Button button23;
        private System.Windows.Forms.Button button22;
        private System.Windows.Forms.Button button21;
        private System.Windows.Forms.Button button20;
        private System.Windows.Forms.Button button19;
        private System.Windows.Forms.Button button18;
        private System.Windows.Forms.Button button17;
        private System.Windows.Forms.Button button16;
        private System.Windows.Forms.Button button15;
        private System.Windows.Forms.Button button14;
        private System.Windows.Forms.Button button13;
        private System.Windows.Forms.Button button12;
        private System.Windows.Forms.Button button11;
        private System.Windows.Forms.Button button10;
        private System.Windows.Forms.Button button9;
        private System.Windows.Forms.Button button8;
        private System.Windows.Forms.Button button7;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;

         public Form1()
        //variables to hold operands
        private double valHolder1;
        private double valHolder2;
        //Varible to hold temporary values
        private double tmpValue;
        //True if "." is use else false
        private bool hasDecimal = false;
        private bool inputStatus = true;
        //variable to hold Operater
        private string calcFunc;

     private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>

Notice string.Empty. Here is System.Entry disassembled:

internal sealed class Empty : ISerializable
    // Fields
    public static readonly Empty Value = new Empty();

    // Methods
    private Empty()

    public void GetObjectData(SerializationInfo info, StreamingContext context)
        if (info == null)
            throw new ArgumentNullException("info");
        UnitySerializationHolder.GetUnitySerializationInfo(info, 1, null, null);

    public override string ToString()
        return string.Empty;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Other Pref. Trust
United States United States
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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Pavel Vladov15-May-10 4:17
memberPavel Vladov15-May-10 4:17 
GeneralMy vote of 1 Pin
Uroš Šmon14-May-10 20:01
memberUroš Šmon14-May-10 20:01 
General! Pin
sam.hill14-May-10 18:44
membersam.hill14-May-10 18:44 
GeneralMy vote of 1 Pin
luisnike1914-May-10 9:55
memberluisnike1914-May-10 9:55 
QuestionWhy? Pin
Matt McKinney14-May-10 9:21
memberMatt McKinney14-May-10 9:21 
GeneralMy vote of 1 Pin
Ajay Vijayvargiya14-May-10 9:08
memberAjay Vijayvargiya14-May-10 9:08 
Bad article, mis leading name (Visual C ?)
GeneralSad Pin
PIEBALDconsult14-May-10 5:25
mvpPIEBALDconsult14-May-10 5:25 
GeneralRe: Sad Pin
luisnike1914-May-10 9:53
memberluisnike1914-May-10 9:53 
GeneralRe: Sad Pin
ring_015-May-10 17:36
memberring_015-May-10 17:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 14 May 2010
Article Copyright 2010 by logicchild
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid