Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Popup Calculator, Button, and Text Control

0.00/5 (No votes)
29 May 2007 1  
A popup calculator form and corresponding button and text controls.

Screenshot - form.jpg

Introduction

I needed a pop-up calculator that could handle its own formatting. Of course, being the cheapskate that I am, I didn't want to pay for it. This calculator, along with the associated button and text controls, can handle parsing and formatting the text value from the control it is associated with. It offers many styling choices, as well.

Using the code

Using the calculator is ridiculously easy: drop a CalculatorButton or CalculatorTextBox on your form. If using the CalculatorButton, you must supply a ResultControl for the button to get and set its text to. About the only things that need explanation are the CalculatorParse and CalculatorFormat events of the CalculatorButton. Essentially, you can intercept these at the time the calculator pops up (CalculatorParse) and/or at the time it closes (CalculatorFormat). Here is some code that shows how you might use these events:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PopCalc.Library;
namespace PopCalc.Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void m_BoxParse_CalculatorParse(
            object sender, CalculatorParseEventArgs e)
        {
            CultureInfo c = CultureInfo.CurrentCulture;
            string cs = c.NumberFormat.CurrencySymbol;
            string ns = c.NumberFormat.NegativeSign;
            string parsed = e.Original;
            if (parsed.Contains(cs))
                parsed = parsed.Replace(cs, "");
            if (parsed.StartsWith("(") && parsed.EndsWith(")"))
                parsed = ns + parsed.Replace("(", "").Replace(")", "");
            e.Parsed = parsed;
        }
        private void m_BoxParse_CalculatorFormat(
        object sender, CalculatorFormatEventArgs e)
        {
        e.FormattedResult = e.Result.ToString("c");
        }
    }
}

The CalculatorParseEventArgs class has an Original property (string, read-only) that is the value that came from the control (usually a TextBox) that the calculator is associated with. You can look at this Original property and change the text, then set the Parsed property (string, read-write) of the event argument.

The CalculatorFormatEventArgs class has a Result property (double, read-only) that you can use to set the FormattedResult property (string, read-write). To reiterate, the CalculatorParse event is called before the text is retrieved from your control; the CalculatorFormat event is called before pushing the result back into your control.

Points of interest

The most interesting thing about writing this code is that I found you have much more flexibility in formatting things if you inherit from Control rather than a more derived class. Thus, the CalculatorButton does not inherit from Button, but rather from Control.

History

  • Version 1.0, released 4/10/07.
  • Updated. Added culture handling for decimal separator, compacted the display a bit, and changed AutoScale = Font on the CalculatorPanel to None so that font changes to the buttons will not change the size of the form panel.
  • Updated 4/11/2007. Calculator will now move to the top of the control if it senses that it has hit the bottom of the screen working area.
  • Updated 4/11/2007. Fixed positioning bugs when used in a non-modal MdiClient form.
  • Updated 5/29/2007. Article edited and posted to the main CodeProject.com article base.

Screenshot - form_2.jpg

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here