如果用户同时输入十六进制和十进制值,则进行数据验证. [英] Data Validation if user keys both Hex as well as decimal values.

查看:67
本文介绍了如果用户同时输入十六进制和十进制值,则进行数据验证.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
在我的WPF应用程序中,我需要验证从十六进制到dec格式的数据,
My.Xaml
< TextBox Grid.Column ="0" Grid.Row ="1" Name ="ErrorList" Horizo​​ntalAlignment ="Left" VerticalAlignment ="Center"> ErrorList</TextBox>
我有一个按钮启动扫描"
My.Xaml.cs
....
....
int MyFunction_InitiateScan(int value)
{

_ScanValue.ErrorList = Convert.ToByte(ErrorList.Text);//如果给定十进制值,它将验证数据.

......
}
我需要验证用户给出的数据的十进制范围(0-7)和十六进制dec(0x-0x7),在wpf中是否有等效的转换和验证来处理上述情况.
如果给出的值超出上述指定范围,则应显示无效数据.我该如何调整以上功能.
在此先感谢.
问候,
Samanth_90

Hi All,
In my WPF Application, I need to validate the data from hex to dec format,
My.Xaml
<TextBox Grid.Column ="0" Grid.Row="1" Name="ErrorList" HorizontalAlignment="Left" VerticalAlignment="Center">ErrorList</TextBox>
I have a button "Initiate Scan"
My.Xaml.cs
....
....
int MyFunction_InitiateScan(int value)
{

_ScanValue.ErrorList= Convert.ToByte(ErrorList.Text);//This validates the data if given decimal value.

......
}
I need to validate the data given by user in decimal range (0- 7) and Hex-dec(0x-0x7), Is there a equivalent conversion and validation in wpf to handle the above scenario.
If given beyond the above specified range it should display Invalid data . how could I tweak the above function.
Thanks in Advance.
With Regards,
Samanth_90

推荐答案

不要在十六进制或十进制之间进行任何区别.您的数字看起来一样.为什么需要"0x"前缀?这不是\十六进制表示法的一部分,这只是字面量是某些编程语言的常用标记.为什么在UI中会需要它,尤其是在(0到7)范围内?

最好执行以下操作:从处理事件PreviewTextInput的输入中过滤掉除0到7和退格键(0x8)以外的所有字符.有关过滤掉输入的技术,请参见:
http://msdn.microsoft.com/en-us/library/system. windows.uielement.previewtextinput.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms752279.aspx [ ^ ].

当您需要使用输入的值时,请解析字符串byte.TryParse.如果失败(调用返回false),则验证将以错误的数字格式失败(实际上,当您使用过滤时,只有在用户输入太多字符时才会发生;可以限制长度(甚至限制为1个字符)) ,使用System.Windows.Controls.TextBox.MaxLength,则根本不会出现此问题.)

在下一步中,验证返回的值在有效范围内.同样,使用范围(0到7),过滤和有限的长度,您将不会遇到此问题.

实际上,对于此特定范围,您不需要任何验证(惊奇!).

或者,为什么您需要小范围的文本框和验证信息?使用数字微调器控件会更好.请参阅:
http://code.google.com/p/phoenix-control-library/ [ ^ ].

祝你好运,
—SA
Don''t make any difference between hex or decimal. Your figures look the same. Why do you need ''0x'' prefix? This is not \a part of hexadecimal notation, this is just a usual marker for the literal is some programming language. Why would you need it in UI, especially in the range (0 to 7)?

Better do the following: filter out all characters except 0 to 7 and backspace (0x8) from input handling the event PreviewTextInput. For the technique of filtering out of the input, please see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewtextinput.aspx[^],
http://msdn.microsoft.com/en-us/library/ms752279.aspx[^].

When you need to use the entered value, parse the string byte.TryParse. In case of failure (the call returns false) fail the validation as a wrong numeric format (in practice, when you use filtering, it could only happen if the user inputs too many characters; you can limit the length (even by 1 character), using System.Windows.Controls.TextBox.MaxLength, then you won''t have this problem at all).

On next step, validate that the returned value is in the valid range. Again, with the range (0 to 7), filtering and limited length you won''t have this problem.

Practically, for this particular range, you won''t need any validation (surprise!).

Alternatively, why would you ever need text box and validation mess for small ranges? You would be much better off with the Numeric Spinner Control. Please see:
http://code.google.com/p/phoenix-control-library/[^].

Good luck,
—SA


这篇关于如果用户同时输入十六进制和十进制值,则进行数据验证.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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