码迷,mamicode.com
首页 > Windows程序 > 详细

WPF TextBox 输入限制小数点后两位

时间:2019-06-20 17:09:24      阅读:642      评论:0      收藏:0      [点我收藏+]

标签:tar   send   amount   code   parse   else   ima   小数点   private   

private void TextBox_OnPreviewKeyUp(object sender, KeyEventArgs e)
{
    var textBox = e.OriginalSource as TextBox;
    if (textBox != null)
    {
        if (!string.IsNullOrWhiteSpace(textBox.Text))
        {
            if (textBox.Text.Substring(textBox.Text.Length-1) != ".")
            {
                if (!textBox.Text.Contains("."))
                {
                    textBox.MaxLength = 10;
                }

                if (decimal.TryParse(textBox.Text, out var anyAmount))
                {
                    // Text成功转为deciml后逻辑
                }
            }
            else
            {
                textBox.MaxLength = textBox.Text.Length + 2;
            }
        }
        else
        {
            // Text为空逻辑
        }
    }
}

WPF TextBox 输入限制小数点后两位

标签:tar   send   amount   code   parse   else   ima   小数点   private   

原文地址:https://www.cnblogs.com/MingsonZheng/p/11059701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!