WPF C# Text box with hour string format - c#

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.

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.

Textbox copy paste handling for numbers vs varchar data

I have thousands of text boxes in my winforms application.
I want to disable copy pasting for text boxes for numeric values for text boxes which should allow only numbers (integers).
For ex:
Employee number: 1234567
I tried setting false to the ShortcutsEnabled property of text box , it completely stops disabling the entire copy & paste, which i don't want, i want to allow copy pasting for numbers.
I can get data from clipboard
Clipboard.GetText();
and check whether
Int32.TryParse
to check number/string.
Is there any general approach that i can use instead of going and pinning the code for each code or use regex expression ?
Use a MaskedTextBox or NumericUpDown instead of a TextBox.
Once simple approach to not disabling at all is to just use a numeric masked textbox that way bad input (non-numeric) will be rejected. Not only for copypaste but for direct input also.
Use TextChanged event and check the validate of the current text in it
and if you are going to use that textbox in many win forms so I suggest you create an advance user control of TextBox and overide its TextChanged method.

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

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;

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.

Validation to have only numeric data in a textbox in a window form

I want that a user only enters numeric data into the textbox in a windows form. How can I achieve this?
The easiest way would be to use a MaskedTextBox (use the Mask property) or a NumericUpDown control. If you really need fine-grained control that these controls do not provide, handle the KeyPress and other appropriate events of the TextBox control as required.
EDIT: Clarified that KeyPress is not the only relevant event, as mentioned by ho1.
What do you want to happen when the user attempts to enter a non-numeric value? Does this matter before a data submission attempt? Without entering your code block every time a key is pressed, I think it'd be cleaner to just let the user enter non-numerics and validate either when the form is submitted and/or when the text box loses focus. Then if validation fails, notify the user of the strict numeric format. This would be less invasive to a user, rather than interrupting.
As for validation, just use either regular expressions or try to parse the text box's text as an integer.
Regular Expression
System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), #"^\d$");
Integer Parse
int validNumbers = 0;
bool isValid = int.TryParse(myTextBox.Text, out validNumbers);
First it depends on what kind of numeric data you want to allow. (Integer, Double, ..., or something app-specific? Like an ISBN or something?)
Short:
The easiest way would be like Ani said, but if you need a more specific way, you should subcribe an matching Event. For Example TextBox.Validating, TextBox.OnLeave, TextBox.OnTextChange... depends on when you want to test the matching.
Than you can test in the Eventhandler whatever you want, even RegEx would be possible for complex alphanumeric data.
PS: You should really have a look at http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx

Categories

Resources