ARTICLE

Scientific Calculator in VB.NET

Posted by Sapna Articles | Visual Basic Language December 03, 2010
This article shows you the programming of a simple scientific calculator in Visual Basic.
Download Files:
 
Reader Level:

A simple scientific calculator perform basic math calculations such as add, subtract, multiply, squareroot, percentage etc and also used for trigonometric calculation such as tangent, square, sine, cosine, absolute value etc. Here is the code snippets to design a simple scientific calculator in Visual Basic.

Scientific Calculator

scientific-cal.gif

Use the following code on each button which hold numbers that are needed in binary operations and the operation to be performed, such as division or multiplication.

namespace Calculator_GUI
{
  public partial class Form1 : Form
  {
    bool isOperation;
    string tempSign; 
    public Form1()
      {

         Text = "Calculator 1.0";
         MaximizeBox = false;    
         InitializeComponent();
      }

   // using button number 1
   private void button1_Click(object sender, EventArgs e)
   {
     but_backspace.Enabled = true;              
     but_backspace.BackColor = Color.LightSkyBlue;        
     if (isOperation == true)                             
       {textBox_output.Text = "";}                    
     if (tempSign == "Sminus")
       {textBox_output.Text += "-1"; }     
     else
     textBox_output.Text += "1";       
     isOperation = false;                    
    }
}

Here is the programming for buttons that are used for performing binary operation like (+, -, *, / and exp) in turn.

private void button16_Click(object sender, EventArgs e)
 {
   if (textBox_output.Text == "")
    {
    
// to prevent exception appearing
    }
   else
    {
      switch (tempSign)
       {
          case "plus":
            Operations.add(double.Parse(textBox_output.Text));
            tempSign = "";
            break;
          case "minus":
            Operations.sub(double.Parse(textBox_output.Text));
            tempSign = "";
            break;
          case "Sminus"
            Operations.Ssub(Double.Parse(textBox_output.Text));
            tempSign = "";
            break;
          case "mult":
            Operations.mult(double.Parse(textBox_output.Text));
            tempSign = "";
            break;
          case "div":
            Operations.div(double.Parse(textBox_output.Text));
            tempSign = "";
            break
          case "modulo":
            Operations.modulo(double.Parse(textBox_output.Text));
            tempSign = "";
            break;
       }
       textBox_output.Text = Convert.ToString(Operations.getResult());
    }
       but_backspace.Enabled = false;        
       but_backspace.BackColor = Color.GhostWhite;  
}

Now Create an event method for handling every trigonometric event like sin, cos, tan etc.

private void button19_Click(object sender, EventArgs e)
  {
    if (textBox_output.Text == "")
      {
       // to prevent exception appearing
      }
    else
      {
        Operations.sin(Double.Parse(textBox_output.Text));
        textBox_output.Text = Convert.ToString(Operations.getResult());
      }
  }

I hope this will help you.

 

Login to add your contents and source code to this article
share this article :
post comment
 

thanks

Posted by Ranjeet Bhardwaj Feb 10, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor