110,570
社区成员
发帖
与我相关
我的任务
分享
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '.')
{
var text = ((TextBox)sender).Text.Substring(0, ((TextBox)sender).SelectionStart);
if (Regex.IsMatch(text, @"(\r|\n|^)\d+$")) return; // 数字在行首
var match = Regex.Match(text, @"\d+$");
if (match.Success)
{
((TextBox)sender).SelectionStart = ((TextBox)sender).SelectionStart - match.Value.Length;
((TextBox)sender).SelectionLength = match.Value.Length;
((TextBox)sender).SelectedText = Environment.NewLine + match.Value;
}
}
}