Skip to content

Instantly share code, notes, and snippets.

@k4m4r82
Created December 4, 2014 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4m4r82/f556c52c034c166121af to your computer and use it in GitHub Desktop.
Save k4m4r82/f556c52c034c166121af to your computer and use it in GitHub Desktop.
Demo Thousand Separator
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