1
我有一个数字文本框分开这样的数字(###,###,###)并使用此代码:使数字文本框像(###,###,###)
private void txtnum2_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
txtnum2.Text += Convert.ToString(e.KeyChar);
}
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
txtnum2.Text = Regex.Replace(txtnum2.Text, "[^.0-9]", "");
for (int i = 1, j = 0; i < 10; ++i)
{
try
{
txtnum2.Text = txtnum2.Text.Insert(txtnum2.Text.Length - ((3 * i) + j), ",");
++j;
}
catch { }
}
但是在第一个“,”之后,其他数字转到文本框的第一个。
您正在使用的WinForms?如果是这样 - 看看'MaskedTextBox':http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox(v=vs.110).aspx –
请考虑upvoting和标记答案为那些帮助或解决您的问题的解决方案。如果他们没有完全达到目标,请随时留下评论。 – paqogomez