Click here to Skip to main content
Licence 
First Posted 18 Nov 2004
Views 100,312
Bookmarked 20 times

Automatically put decimal for currency format in TextBox when typing

By | 18 Nov 2004 | Article
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

About the Author

gitansu



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralACTIVATE NUM-LOCK PAD. PinmemberMember 33216378:35 28 Dec '08  
GeneralRe: ACTIVATE NUM-LOCK PAD. PinmemberDavidMills024:08 27 Nov '10  
GeneralCurrency textbox Pinmemberrik rik grinPeace15:31 29 Jul '08  
GeneralRe: Currency textbox Pinmemberrik rik grinPeace19:35 30 Jul '08  
GeneralVB.net version Pinmemberfelic10116:31 5 Sep '07  
' This is VB.net version, similar to the original posting, but with a few additional
' lines of code added.
' Compliments given to the author. Smile | :)
 
Private str As String = ""
Private Sub textbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyDown
            Dim keycode As Integer = e.KeyValue
            If Not IsNumeric(keycode) Then
                  e.Handled = True
                  Return
            Else
                  e.Handled = True
            End If
            If ((keycode = 8 Or keycode = 46) And str.Length > 0) Then
                  str = str.Substring(0, str.Length - 1)
            Else
                  If Not (keycode = 8 Or keycode = 46) Then
                        ' comment: I added these lines to include keys captured with numlock on  
                        If (keycode >= 96 And keycode <= 105) Then
                              keycode -= 48
                        End If
                        str &= Convert.ToChar(keycode)
                  End If
            End If
            If str.Length = 0 Then
                  textbox1.Text = ""
            ElseIf str.Length = 1 Then
                  textbox1.Text = "0.0" & str
            ElseIf str.Length = 2 Then
                  textbox1.Text = "0." & str
            ElseIf str.Length > 2 Then
                  ' comment: ommit character 0 in the front
                  If str.Length >= 4 And str.Substring(0, 1) = "0" Then
                        str = str.Substring(1)
                  End If
                  textbox1.Text = str.Substring(0, str.Length - 2) & "." & str.Substring(str.Length - 2)
            End If
      End Sub
 
      Private Function isNumeric(ByVal val As Integer) As Boolean
            ' comment: added value range from 96 to 105 (numlock)
            Return ((val >= 48 And val <= 57) Or (val >= 96 And val <= 105) Or (val = 8) Or (val = 46))
      End Function
 
      Private Sub textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textbox1.KeyPress
            e.Handled = True
      End Sub
GeneralPasting PinmemberPeter Ritchie4:36 5 Feb '06  
GeneralWorks Great PinmemberTLdraw3:00 9 Jun '05  
GeneralRe: Javascript PinsussDave Gray10:57 19 Nov '04  
GeneralRe: Javascript PinmemberBob Aman11:42 19 Nov '04  
GeneralRe: Javascript PinmemberBob Aman11:43 19 Nov '04  
Questionand what about non US values ? Pinmembermcarbenay2:03 19 Nov '04  
GeneralGRRR>>> Pinmembertom_dx13:47 18 Nov '04  
GeneralRe: GRRR>>> PinsussAnonymous16:02 18 Nov '04  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 18 Nov 2004
Article Copyright 2004 by gitansu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid