Click here to Skip to main content
14,693,117 members
Articles » Desktop Development » Miscellaneous » Miscellaneous Controls
Posted 10 Apr 2007


82 bookmarked

C# Popup Calculator, Button, and Text Control

Rate me:
Please Sign up or sign in to vote.
4.69/5 (31 votes)
29 May 2007CPOL
A popup calculator form and corresponding button and text controls.

Screenshot - form.jpg


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()
        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.


  • 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 article base.

Screenshot - form_2.jpg


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
Bruce Pierson is the CTO of Connexa Softools, Inc. (, a software company specializing in product configuration and build-to-order manufacturing tools.

Comments and Discussions

Questiondesign the form Pin
Member 1024524221-Sep-13 18:36
MemberMember 1024524221-Sep-13 18:36 
GeneralMy vote of 5 Pin
Mohammad Dayyan6-Nov-10 7:29
MemberMohammad Dayyan6-Nov-10 7:29 
GeneralMy vote of 4 Pin
Varun Sareen23-Jul-10 1:31
professionalVarun Sareen23-Jul-10 1:31 
RantWonderful and Excellent Pin
Pouriya Ghamary27-Mar-10 13:49
MemberPouriya Ghamary27-Mar-10 13:49 
GeneralThank you Pin
Ali Azhdari18-May-09 4:06
MemberAli Azhdari18-May-09 4:06 
Generalabout the calc Pin
oribello12-Jun-08 23:21
Memberoribello12-Jun-08 23:21 
GeneralRe: about the calc Pin
beep13-Jun-08 6:30
Memberbeep13-Jun-08 6:30 
Generaladd effect to button Pin
majid soorani8-Dec-07 3:01
Membermajid soorani8-Dec-07 3:01 
Generalabout modal mode Pin
powermetal26-Sep-07 10:48
Memberpowermetal26-Sep-07 10:48 
GeneralThis control rocks! Pin
johnftamburo27-Jun-07 13:25
Memberjohnftamburo27-Jun-07 13:25 
GeneralRe: This control rocks! Pin
beep27-Jun-07 19:47
Memberbeep27-Jun-07 19:47 
GeneralRe: This control rocks! Pin
johnftamburo28-Jun-07 6:15
Memberjohnftamburo28-Jun-07 6:15 
GeneralA few comments Pin
Joe Sonderegger17-Apr-07 2:43
MemberJoe Sonderegger17-Apr-07 2:43 
GeneralRe: A few comments Pin
beep17-Apr-07 11:21
Memberbeep17-Apr-07 11:21 
GeneralRe: A few comments Pin
Joe Sonderegger17-Apr-07 22:03
MemberJoe Sonderegger17-Apr-07 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.