C# Tutorial - A Simple Calculator – More Functionality


Now lets add the rest of the buttons which do not relate to memory storage.

Plus/Minus Button

This button simply needs to change the sign of what is being displayed in the Result label. Double-click on buttonPM to create the default Click event and add the following code:

private void buttonPM_Click(object sender, EventArgs e)
{
  if ((Result.Text != "") || (Result.Text != "0"))
  {
    if (Result.Text.StartsWith("-") == true)
      Result.Text = Result.Text.Substring(1);
    else
      Result.Text = "-" + Result.Text;
  }
}

The code does not need to convert the string to a number to change the sign, instead we can just use some string manipulation.

Square-root Button

This button calculates the square-root of what is being displayed in the Result label. Double-click on buttonSQRT to create the default Click event and add the following code:

private void buttonSQRT_Click(object sender, EventArgs e)
{
  try
  {
    // Get the value being displayed
    double i = double.Parse(Result.Text);

    // Square the value and display it
    Result.Text = (Math.Sqrt(i)).ToString();

    // Clear the Result label on next number entry.
    _clrDisplay = true;
  }
  catch
  {
    // FormatException will occur if Result label is empty
  }
}

Percent Button

The percent button calculates a percentage of the previously stored first value. Double-click on buttonPercent to create the default Click event and add the following code:

private void buttonPercent_Click(object sender, EventArgs e)
{
  try
  {
    // Get the value being displayed
    double i = double.Parse(Result.Text);

    // Square the value and display it
    Result.Text = (_num1 * (i / 100)).ToString();

    // Clear the Result label on next number entry.
    _clrDisplay = true;
  }
  catch
  {
    // FormatException will occur if Result label is empty
  }
}

One Over Button

This button calculates 1 divided by what is being displayed in the Result label. Double-click on buttonOneOver to create the default Click event and add the following code:

private void buttonOneOver_Click(object sender, EventArgs e)
{
  try
  {
    // Get the value being displayed
    double i = double.Parse(Result.Text);

    if( i != 0 )
    {
      // Square the value and display it
      Result.Text = (1 / i).ToString();

      // Clear the Result label on next number entry.
      _clrDisplay = true;
    }
  }
  catch
  {
    // FormatException will occur if Result label is empty
  }
}

Memory Buttons

Create default Click events for each of the memory buttons, then change them to the following code:

private void buttonMC_Click(object sender, EventArgs e)
{
  // Clear memory
  _memory = 0;

  // Update label
  MemText.Text = "0";
}


private void buttonMR_Click(object sender, EventArgs e)
{
  // Set Result label to that of Memory label
  Result.Text = MemText.Text;
}


private void buttonMS_Click(object sender, EventArgs e)
{
  try
  {
    // Store Result in Memory
    _memory = double.Parse(Result.Text);

    // Update memory label
    MemText.Text = Result.Text;
  }
  catch
  {
    // FormatException will occur if Result label is empty
  }
}


private void buttonMP_Click(object sender, EventArgs e)
{
  try
  {
    // Add result to memory
    _memory += double.Parse(Result.Text);

    // Update memory label
    MemText.Text = _memory.ToString();
  }
  catch
  {
    // FormatException will occur if Result label is empty
  }
}

This gives us a simple but functional calculator.


<< Previous Contents Next >>

© Publicjoe, 2008