.net c # textbox with mask - setting
I have a C # .net project and want to enter an input text box for a date value. I want it to display the default mm / dd / yyyy and then allow users to enter valid dates.
I tried using a hidden textbox but it doesn't like having the above format.
If I try to use / / ____ looks cheeky and the same 00/00/0000, and of course if you add '0' like 03/11/2009 you get 3/11/29 because 0 is removed like part of the mask.
What is the best way to set up an input field like this, efficiently with a mask, only allowing numbers and validation (although it doesn't bother me that much at the moment).
It seems so simple and is not.
a source to share
Why not try and use a plugin like jQuery UI DatePicker?
http://docs.jquery.com/UI/Datepicker
They are lightweight, proven and work-saving!
a source to share
Assuming Windows Forms, the DateTimePicker is the tool for the job.
Here's an example with MaskedTextBox , but since you say it doesn't work ...
Another good try if you use DataBinding
if by creating a Binding ( Binding for WPF ) class and handling its Parse and Format events (they don't seem to exist in WPF, but must be somehow).
In short:
Binding b = new Binding("Text", BindingSourceInstance, "data.DateCreated");
b.Parse += new EventHandler(b_Parse);
b.Format += new EventHandler(b_Format);
private void b_Format(object sender, ConvertEventArgs e) {
if (e.DesiredType != typeof(DateTime)) return;
e.Value.ToString("dd/MM/yyyy");
// Or you may prefer some other variants:
// e.Value.ToShortDateString();
// e.Value.ToLongDateString();
// e.Value.ToString("dd/MM/yyyy", CultureInfo.CurrentCulture.DateTimeInfo.ShortDatePattern);
}
private void b_Parse(object sender, ConvertEventArgs e) {
// Choose whether you to handle perhaps bad input formats...
e.Value = DateTime.Parse(e.Value, CultureInfo.InvariantCulture); // The CultureInfo here may be whatever you choose.
}
a source to share
Using a masked text field for the date is a headache, best used DateTime.TryParseExact()
with an acknowledged event control.
It will guarantee the user's input in the correct format without too many encodings, as in this example:
you need to also use ToolTip
to instruct the user to enter correctly.
private void txtEntryDate_Validated(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtEntryDate.Text))
{
DateTime entryDate;
if (DateTime.TryParseExact(txtEntryDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out entryDate))
{
// further actions for validations
}
else
{
setTooltip(txtEntryDate, "txtEntryDate", "Invalid date format date must be formatted to dd/MM/yyyy");
txtEntryDate.Focus();
}
}
else
{
setTooltip(txtEntryDate, "txtEntryDate", "Please provide entry date in the format of dd/MM/yyyy");
txtEntryDate.Focus();
}
}
and the class ToolTip
:
private void setTooltip(Control Ctrl, string CtrlCaption, string ToolTipMsg)
{
ToolTip tt1 = new ToolTip();
tt1.AutoPopDelay = 5000;
tt1.InitialDelay = 1000;
tt1.ReshowDelay = 500;
tt1.ShowAlways = false;
tt1.SetToolTip(Ctrl, CtrlCaption);
tt1.Show(ToolTipMsg, Ctrl,5000);
}
a source to share