Click here to Skip to main content
16,015,444 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 1:36am    
What conversion? What validation? How can it have anything to do with WPF?!
--SA

1 solution

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
 
Share this answer
 
v3
Comments
Espen Harlinn 28-Feb-12 14:18pm    
Nice reply :)
Sergey Alexandrovich Kryukov 28-Feb-12 14:33pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900