Click here to Skip to main content
12,954,403 members (68,351 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 5 Feb 2014

Currency TextBox .NET

, 5 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip describes how to display the value as currency text, formatted in accordance with the current culture.


This tip describes how to display the value as currency text, formatted in accordance with the current culture. MaskedTextBox control is inadequate about this concept. CurrencyTextBox is usercontrol.


I thought of a simple accounting program coding. I decided using MaskedTextBox because it has mask. But I had a new problem. MaskedTextBox control has inadequate and limited input number. I decided to create my own control.

Using the Code

CurrencyTextBox is derived from TextBox control. This usercontrol is using via OnEnter and OnLeave events. You enter input numbers then after leave from CurrencyTextBox, it seems masked for currency. If you want to enter the same control again, it seems no-masked.

Usage Text and TextNoFormatting properties:
For example: if you enter 12345;

Region currencyTextBox.Text currencyTextBox.TextNoFormatting
USA (en-US)$12.345,00 12345,00
Turkey (tr-TR)12.345,00 TL12345,00

 * Author: emarti, Murat Özdemir
 * Date: 5 Feb 2014, v1.0 
 * Updated: 8 Feb 2014, v1.1 (Thanks to johannesnestler from
using System;
using System.Linq;
using System.Windows.Forms;
using System.Globalization;

namespace CurrencyTextBox
    public partial class CurrencyTextBox: TextBox
        readonly CultureInfo _ci = CultureInfo.InstalledUICulture;
        private readonly string _allowedCharacterSet;
        private readonly char _decimalSeparator;

        public CurrencyTextBox()
            var nf = new CultureInfo(_ci.Name, false).NumberFormat;
            _decimalSeparator = nf.CurrencyDecimalSeparator.ToCharArray()[0];
            _allowedCharacterSet = string.Format("0123456789{0}", _decimalSeparator);

        /// <summary>
        /// Return Currency Text with No Formatting
        /// </summary>
        public string TextNoFormatting
            get { return TypedText(); }

        protected override void OnLeave(EventArgs e)
            double amount;
            Text = Double.TryParse(Text, NumberStyles.Currency, null, 
            out amount) ? amount.ToString("C") : 0.ToString("C");

        private string TypedText()
            var sonuc = string.Empty;
            return Text.Trim().Where(ch => _allowedCharacterSet.Contains(ch)).Aggregate
            (sonuc, (current, ch) => current + ch);

        protected override void OnEnter(EventArgs e)
            Text = TypedText();

        protected override void OnKeyPress(KeyPressEventArgs e)
            if (!char.IsDigit(e.KeyChar) && 
            e.KeyChar != _decimalSeparator && !char.IsControl(e.KeyChar))
                e.Handled = true;

Using the Control

CurrencyTextBox is a user control that you can add to your form. It behaves the same as a regular TextBox.

To use the control:

  • Right-click on your toolbox in Design view.
  • Click Browse. Navigate to CurrencyTextBox.dll. Hit OK.
  • Other alternative; add reference your project.
  • Add the control to your form.  
  • Enjoy!


  • Version 1.0, Created 5 Feb 2014
  • Version 1.1 Created 8 Feb 2014
    • Code optimized
    • Added OnKeyPress Event, allowed to input (enter) numbers and decimal separator character only
    • Changed property syntax from 'Text_NoFormatting' to 'TextNoFormatting'  


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


About the Author

Turkey Turkey
My name is Murat Özdemir. I'm a Turkish Dentist and living in Eskişehir (Turkey).
Software programming is one of my hobbies since 1990 and I like to deal with 1 and 0.
Do not forget to brush your teeth for your health. Smile | :)
Visit my homepage at

You may also be interested in...

Comments and Discussions

Questiona Little better Pin
johannesnestler7-Feb-14 0:14
memberjohannesnestler7-Feb-14 0:14 
AnswerRe: a Little better Pin
emarti7-Feb-14 5:09
memberemarti7-Feb-14 5:09 
AnswerRe: a Little better Pin
emarti7-Feb-14 6:30
memberemarti7-Feb-14 6:30 
GeneralRe: a Little better Pin
johannesnestler7-Feb-14 9:09
memberjohannesnestler7-Feb-14 9:09 
GeneralRe: a Little better Pin
emarti8-Feb-14 1:38
memberemarti8-Feb-14 1:38 
GeneralMy vote of 2 Pin
johannesnestler7-Feb-14 0:03
memberjohannesnestler7-Feb-14 0:03 
GeneralRe: My vote of 2 Pin
emarti7-Feb-14 5:06
memberemarti7-Feb-14 5:06 
GeneralRe: My vote of 2 Pin
johannesnestler7-Feb-14 9:15
memberjohannesnestler7-Feb-14 9:15 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 5 Feb 2014
Article Copyright 2014 by emarti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid