Created
December 4, 2014 05:46
-
-
Save k4m4r82/f556c52c034c166121af to your computer and use it in GitHub Desktop.
Demo Thousand Separator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
txtThousandSeparator.TextAlign = HorizontalAlignment.Right; | |
} | |
private void SetThousandSeparator(TextBox txt) | |
{ | |
if (txt.Text.Length > 0) | |
{ | |
try | |
{ | |
string s = txt.Text.Replace(",", ""); | |
int i = int.Parse(s); | |
txt.Text = string.Format("{0:###,###,###}", i); | |
txt.SelectionStart = txt.Text.Length; | |
} | |
catch | |
{ | |
} | |
} | |
} | |
private bool NumericOnly(System.Windows.Forms.KeyPressEventArgs e) | |
{ | |
string strValid = "0123456789."; | |
if (strValid.IndexOf(e.KeyChar) < 0 && !(e.KeyChar == Convert.ToChar(Keys.Back))) | |
{ | |
return true; // not valid | |
} | |
else | |
{ | |
return false; // valid | |
} | |
} | |
private void txtThousandSeparator_TextChanged(object sender, EventArgs e) | |
{ | |
SetThousandSeparator((TextBox)sender); | |
} | |
private void txtThousandSeparator_KeyPress(object sender, KeyPressEventArgs e) | |
{ | |
e.Handled = NumericOnly(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment