Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / Windows Forms
Article

C# Popup Calculator, Button, and Text Control

Rate me:
Please Sign up or sign in to vote.
4.69/5 (31 votes)
29 May 2007CPOL2 min read 112.2K   4.5K   84   15
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
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.

Comments and Discussions

 
Questiondesign the form Pin
Member 1024524221-Sep-13 17:36
Member 1024524221-Sep-13 17:36 
GeneralMy vote of 5 Pin
Mohammad Dayyan6-Nov-10 6:29
Mohammad Dayyan6-Nov-10 6:29 
GeneralMy vote of 4 Pin
Varun Sareen23-Jul-10 0:31
Varun Sareen23-Jul-10 0:31 
RantWonderful and Excellent Pin
Pouriya Ghamary27-Mar-10 12:49
Pouriya Ghamary27-Mar-10 12:49 
GeneralThank you Pin
Ali Azhdari18-May-09 3:06
Ali Azhdari18-May-09 3:06 
Generalabout the calc Pin
oribello12-Jun-08 22:21
oribello12-Jun-08 22:21 
GeneralRe: about the calc Pin
beep13-Jun-08 5:30
beep13-Jun-08 5:30 
Generaladd effect to button Pin
majid soorani8-Dec-07 2:01
majid soorani8-Dec-07 2:01 
Generalabout modal mode Pin
powermetal26-Sep-07 9:48
powermetal26-Sep-07 9:48 
GeneralThis control rocks! Pin
johnftamburo27-Jun-07 12:25
johnftamburo27-Jun-07 12:25 
GeneralRe: This control rocks! Pin
beep27-Jun-07 18:47
beep27-Jun-07 18:47 
GeneralRe: This control rocks! Pin
johnftamburo28-Jun-07 5:15
johnftamburo28-Jun-07 5:15 
GeneralA few comments Pin
Joe Sonderegger17-Apr-07 1:43
Joe Sonderegger17-Apr-07 1:43 
GeneralRe: A few comments Pin
beep17-Apr-07 10:21
beep17-Apr-07 10:21 
GeneralRe: A few comments Pin
Joe Sonderegger17-Apr-07 21:03
Joe Sonderegger17-Apr-07 21: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.