Click here to Skip to main content
Click here to Skip to main content

Automatically put decimal for currency format in TextBox when typing

, 18 Nov 2004
Rate this:
Please Sign up or sign in to vote.
Automatically put decimal for currency format in TextBox when typing.

Introduction

This is a cool code to use TextBox for entering currency data. It will automatically add decimal when typing on the TextBox. The backspace and delete key is used to delete a number from the TextBox.

The source code is:

string str="";
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
  int KeyCode = e.KeyValue;

  if(!IsNumeric(KeyCode))
  {
    e.Handled=true;
    return;
  }
  else
  {
    e.Handled=true;
  }
  if(((KeyCode==8)||(KeyCode==46))&&(str.Length>0))
  {
    str = str.Substring(0,str.Length-1);
  }
  else if(!((KeyCode==8)||(KeyCode==46)))
  {
    str = str+Convert.ToChar(KeyCode);
  }
  if(str.Length==0)
  {
    textBox1.Text = "";
  }
  if(str.Length==1)
  {
    textBox1.Text = "0.0"+str;
  }
  else if(str.Length==2)
  {
    textBox1.Text = "0."+str;
  }
  else if(str.Length>2)
  {
    textBox1.Text = str.Substring(0,str.Length-2)+"."+
                    str.Substring(str.Length-2);
  }
}

private bool IsNumeric(int Val)
{
  return ((Val>=48 && Val<=57) || (Val == 8) ||(Val==46));
}

private void textBox1_KeyPress(object sender, 
        System.Windows.Forms.KeyPressEventArgs e)
{
  e.Handled=true;
}

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

Share

About the Author

gitansu

India India
No Biography provided

Comments and Discussions

 
GeneralACTIVATE NUM-LOCK PAD. PinmemberMember 332163728-Dec-08 9:35 
GeneralRe: ACTIVATE NUM-LOCK PAD. PinmemberDavidMills0227-Nov-10 5:08 
GeneralCurrency textbox Pinmemberrik rik grinPeace29-Jul-08 16:31 
GeneralRe: Currency textbox Pinmemberrik rik grinPeace30-Jul-08 20:35 
GeneralVB.net version Pinmemberfelic1015-Sep-07 17:31 
GeneralPasting PinmemberPeter Ritchie5-Feb-06 5:36 
GeneralWorks Great PinmemberTLdraw9-Jun-05 4:00 
GeneralRe: Javascript PinsussDave Gray19-Nov-04 11:57 
GeneralRe: Javascript PinmemberBob Aman19-Nov-04 12:42 
GeneralRe: Javascript PinmemberBob Aman19-Nov-04 12:43 
Questionand what about non US values ? Pinmembermcarbenay19-Nov-04 3:03 
GeneralGRRR&gt;&gt;&gt; Pinmembertom_dx18-Nov-04 14:47 
GeneralRe: GRRR&gt;&gt;&gt; PinsussAnonymous18-Nov-04 17:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 18 Nov 2004
Article Copyright 2004 by gitansu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid