Click here to Skip to main content
11,705,052 members (52,809 online)
Click here to Skip to main content

Automatically put decimal for currency format in TextBox when typing

, 18 Nov 2004 126.4K 1.3K 21
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

You may also be interested in...

Comments and Discussions

 
GeneralACTIVATE NUM-LOCK PAD. Pin
Member 332163728-Dec-08 8:35
memberMember 332163728-Dec-08 8:35 
GeneralRe: ACTIVATE NUM-LOCK PAD. Pin
DavidMills0227-Nov-10 4:08
memberDavidMills0227-Nov-10 4:08 
GeneralCurrency textbox Pin
rik rik grinPeace29-Jul-08 15:31
memberrik rik grinPeace29-Jul-08 15:31 
GeneralRe: Currency textbox Pin
rik rik grinPeace30-Jul-08 19:35
memberrik rik grinPeace30-Jul-08 19:35 
GeneralVB.net version Pin
felic1015-Sep-07 16:31
memberfelic1015-Sep-07 16:31 
GeneralPasting Pin
Peter Ritchie5-Feb-06 4:36
memberPeter Ritchie5-Feb-06 4:36 
GeneralWorks Great Pin
TLdraw9-Jun-05 3:00
memberTLdraw9-Jun-05 3:00 
GeneralRe: Javascript Pin
Dave Gray19-Nov-04 10:57
sussDave Gray19-Nov-04 10:57 
GeneralRe: Javascript Pin
Bob Aman19-Nov-04 11:42
memberBob Aman19-Nov-04 11:42 
GeneralRe: Javascript Pin
Bob Aman19-Nov-04 11:43
memberBob Aman19-Nov-04 11:43 
Questionand what about non US values ? Pin
mcarbenay19-Nov-04 2:03
membermcarbenay19-Nov-04 2:03 
GeneralGRRR&gt;&gt;&gt; Pin
tom_dx18-Nov-04 13:47
membertom_dx18-Nov-04 13:47 
GeneralRe: GRRR&gt;&gt;&gt; Pin
Anonymous18-Nov-04 16:02
sussAnonymous18-Nov-04 16: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
Web02 | 2.8.150819.1 | Last Updated 18 Nov 2004
Article Copyright 2004 by gitansu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid