使用wpf在C#中进行文本框验证 [英] Text box validation in C# with wpf

查看:213
本文介绍了使用wpf在C#中进行文本框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,其中用户输入的时间类似于ex:10.5或2.5,所以当用户输入10或2时,文本框将显示.0作为附加到10或2。

,最后文本框显示为10.0或2.0的值。



所以textBox的最大长度是4.所以当用户输入像1111,文本框将附加.0。如何限制文本框的长度,使其只接受ex:1.2或10.5或12.0或1.0之类的值。并且不接受像1111.0这样的值



我正在使用以下代码来附加.0



I have a text box in which user enters hours like ex: 10.5 or 2.5 , so when the user just enters 10 or 2, then the text box will display ".0" as appended to 10 or 2.
and finally the text box will displayed as value as 10.0 or 2.0.

So the maximum lenghtof textBox is 4. so when the user enters values like 1111, the textbox will be appended with ".0". How to restrict the lenght of textbox so that it will accept only values like ex: 1.2 or 10.5 or 12.0 or 1.0. and not to accept values like 1111.0

am using the follwoing code to code to append ".0"

private void txtPlannedHours_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            txtPlannedHours.Text = Convert.ToDouble(txtPlannedHours.Text).ToString("0.0");
        }





使用以下正则表达式:

Regex re = new Regex( ^ \\d {1,2}([。] \\\\ {1})$);



任何人都可以建议我?



And am using the following regular expression:
Regex re = new Regex("^\\d{1,2}([.]\\d{1})$");

can anyone please suggest me?

推荐答案

);



有人可以建议我吗?
");

can anyone please suggest me?

检查此链接是否有帮助



www.codeproject.com/Articles/34228/WPF-Maskable-TextBox-For-Numeric-Values
Check this link it will be of help

www.codeproject.com/Articles/34228/WPF-Maskable-TextBox-For-Numeric-Values


首先,这不是在WPF中正确编码的最好方法。

我同意这是有效的,但你应该使用的是数据绑定。



使用数据绑定在UI中显示此数据。

我将让您探索有关数据绑定的更多信息 - 数据绑定(WPF) [ ^ ]。



绑定数据后,您可以使用验证规则验证数据,例如 http://msdn.microsoft.com/en-us/ library / ms752347%28v = vs.110%29.aspx #data_validation [ ^ ]。
First of all, this is not the best way to right code in WPF.
I agree this works but what you should be using is Data Binding.

Display this data in the UI by using data binding.
I will leave you to explore more about data binding - Data Binding (WPF)[^].

Once you bind data, you can validate data using Validation rules for e.g. http://msdn.microsoft.com/en-us/library/ms752347%28v=vs.110%29.aspx#data_validation[^].


这篇关于使用wpf在C#中进行文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆