How can I restrict the values a user enters into TextBox? - c#

Users of my application can enter values into TextBoxes. In some cases the values need to be restricted to a certain range, e.g. 0 to 1, or 1 to 50. In other cases they can only enter upto 13 or 20 characters depending.
Can I use Binding.StringFormat to enforce these ranges? If so,
what do I need to specify to restrict values to a certain character length?
what do I need to specify to restrict values to a certain range?
If not, what's the best way to implement this?

What you want is input validation.
For starters this tutorial on MSDN will help you, more information on IDataErrorInfo (preferred way) can be obtained here.
This question was already a topic on SO: WPF Data Binding and Validation Rules Best Practices

The Vaildation in WPF Code Project should give you a good start.
Also the related question WPF Validators like ASP.NET should assist.

You need to use Input mask, Check out this article WPF Maskable TextBox for Numeric Values
Based on this article in Events TextBox_PreviewTextInput and TextBoxPastingEventHandler you could check for a range.

The text box length property should solve
Textbox.maxlength= 10;

Related

Devexpress RegEx Mask with different Displayformat doesn't work

I have a Textedit Control that I want to behave in a certain way:
When the Control has input focus, I want to only allow the input of positive whole Numbers (not zero). I achieve that by using Properties.Mask, which works fine.
When the Control does NOT have input focus, I want it to Display the entered number but with ",00" at the end.
So basically, while I enter something, I only see what I enter e.g. "17" but when the Control loses focus, I want it to show "17,00". So that I am only allowed to enter whole positive numbers but the Control will always add ",00" afterwards.
My understanding is that there are basically two different "modes": DisplayMode and EditMode.
EditMode = The Control has focus.
DisplayMode = The Control does not have focus.
In EditMode, I can type things into my Textedit Control. What I can and can not enter is determined by the Mask.
When I lose focus, it goes into DisplayMode. Here I cant type anything into the TextEdit but now the displayed text is not determined by the mask any more but by the Property "Properties.DisplayFormat". So to achieve my goal, I tried to set the DisplayFormat.FormatString to "0.00", so that it would always show two decimal places "x,00".
Somehow, this doesn't work as expected. The DisplayFormat doesn't seem to do anything and even in DisplayMode, the TextEdit still shows just the whole number without the decimal places.
I realize that I could use events to work around this problem but I think that's what DisplayFormat, EditFormat and Mask are for and I really don't want to handle multiple events for something that small.
Accodring to DevExpress Knowledge Base topic DisplayFormat is not working in unbound mode.
Problems with formatting occurs because an unbound text editor stores
a value as a string, therefore formatting cannot be applied.
If you use XtraEditors 3 or higher, you may wish to set the editor's
Mask.MaskType property to Numeric. In this case, the editor is forced
to handle the edit value as a number and, therefore, it can format it.
If you wish not to use the Numeric (or DateTime) mask, please use the
ParseEditValue event to convert a string to a number.
I can suggest you to use Numeric mask with n0 as edit mask:
To accomplish the above is fairly simple:
To only allow positive whole numbers, you need to set the MaskType to Numeric and use the EditMask ##########;. The number of # represents the possible number of didgets so ten times # means you can use a ten-digit number. (see nempoBu4's answer)
To show an additional ,00 when the control loses the focus, you simply need to set the DisplayFormat as FormatType = Numeric and FormatString = n2.

Compare multiple textbox - Make sure 1 is entered

In my Content page I have 4 different TextBox's which is used to enter phone number - Mobile, Office Phone, Office Mobile & Other. I have RegularExpressionValidator for each.
I want to make sure atleast one of the above 4 textbox's has entered a value. I didn't find any example or article on net showing this situation. One way I think is in Submit button click, before Page.IsValid call, call a function that checks that one of the control has valid value. If the function returns false, show MessageBox. Is their any other way using Validators or so to comfirm that one textbox out of 4 has a valid value.
What can be the best way to achieve this ?
Any help is highly appreciated.
Thankss
You can create a Custom Validator in ASP.NET.
This post should help you out.
asp.net required field validator for at least one textbox contains text

How to build a "word-by-word" autocomplete in WPF?

I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.

WPF C# Text box with hour string format

How to make that user can only input hour format in text box for example HH:MM?
Depending on strictness of requirements you may want to create a specific behaviour that will be attached to KeyDown event and disable keys other than currently accepted given format and current text input position in (HH:MM), you could even think of different ways to guarantee automatic : input by your behaviour if you want to be limited by textbox control for time input.
you can get some inspiration here I think
numeric only behaviour example
Look at Binding.StringFormat. It allows for conversion to DateTime or TimeSpan while specifying a certain string format to define how the user inputs the value, and how the value is displayed.
It sounds like you are looking for a masked text box.
You can fine one on codeplex here...
WPF Toolkit
EDIT.
There is also a datetimeupdown control. Which might be nearer to what you want.

Developing a custom-validation in asp.net for specific control and criteria

There is another relevant question asked Validation Check in asp.net
In the same scenario we need a custom validator control which will alert user for any wrong entry. This will work like this :
Developer will pass the control-name, input-value and format-required
For instance like for textbox it can be: txtName,txtName.Text, allow-alphabets-only
The accordingly format if the user input is invalid he/she will be got prompt.
Please suggest the right way to do the smae.
Thanks in advance.
You've described the default behaviour of all asp.net validators (pass control to validate etc).
What you want to achive (eg, alpha charaters only, or alphanumeric only etc) can be achieved mostly with the RegularExpressionValidator.

Categories

Resources