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
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:
public partial class Form1 : Form
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;
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");
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.
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
- 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.
Bruce Pierson is the CTO of Connexa Softools, Inc. (www.connexatools.com), a software company specializing in product configuration and build-to-order manufacturing tools.