Implement Scientific Calculator using VB.NET

This article shows you the programming of a simple scientific calculator in Visual Basic.
  • 20248
 

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());
      }
  }

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.