How to modify WPF DatePicker to accept any text input? - c#

Is there a way to modify the WPF DatePicker so that the user can enter non dates (e.g period codes such as 1Y) in addition to dates?
Basically I'm looking for a text entry field that can take any string (or optionally a string that matches a custom validation rule) that has a calendar icon next to it, which when used will paste the selected date into that text field...

Related

Date format PATTERN validation, not date validation

I have a TextBox where the user is able to enter in their preferred date format pattern (e.g. dd/M/yyyy, yy-dd-mm).
How can I validate this Textbox? I could do it on the server-side by setting the Text property of this Textbox to cultureInfo.DateTimeFormat.ShortDatePattern and then catching the thrown exception, but I want to do this on the client-side using ASP.NET validators (a CompareValidator or RegularExpressionValidator).
This is a very difficult problem to search for as all that comes up are date format validation problems.

best way to handle time validation for textbox

Hello my WinForm application has a textbox that will be used for a time value. I know how to load like the current time into the textbox, but say when a user enters a value, what is the best way to validate or format the number so I can attempt to Convert it to a TimeDate value?
Example:
User enters something like 800 should be able to be converted to 8:00am or 2354 should be 11:54pm. Also if they enter an invalid time, like 9cdf83 or something, I need to check that this value is convertable to a time. If not, throw a warning. Is there a way to display a system clock to choose the time from like the dateTimePicker?
For things like date and time which u want user to enter best is to use DateTimePicker bcoz n number of users will have n number of ideas to fill it. u can't go on checking each and every option.
Instead DateTimePicker will make user to enter through it only.
you can place the textbox and a label beside it specifying the format like 8:00 AM and for validating it you can use custom validation and use a suitable regular expression as required for the validator and attach it to textbox.
But For conversion I thing you will have to code and check its validity.

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.

Avoid pressing . (period) key to move to next field in DateTime picker control

In datetime picker control the user has to press .(period) to move to different fields (like day, month year)
I want to override this behaviour by allowing the user to enter date without entering .(period)
Is there any to achieve this programmatically in C# winforms?
Thanks in advance.
Not very beautiful code but you could of course send the . automatically.
Just handle the ValueChanged event (which will only be called when the user has entered a "whole" value for date part he's currently editing) and in the event handler you add the code SendKeys.Send(".");.

C# form custom textbox format binding

I haev an object with a DateTime property. I want to map the time to a textbox (thus want the user enter the time, it's directly reverberated to my property).
However I just want to display the time. How can I say this (ie the format should be something like hh:mm to be correct).
Less specifcly how can I format the text "sent" to the object and the other way too ?
Thx
You can specify "HH:mm" as the binding's FormatString
If you need more complex formatting than what a format string can provide, you can handle the Format event of the binding, which enables you to provide your own formatting logic

Categories

Resources