I always hit a wall when trying to present the user with the possibility to enter a time (in format hh:mm or HH:mm). It ain’t an easy task and the sollutions are countless, but I won’t get into them. Instead I will unleash the power of the DateTimePicker upon you
.
What does a DateTimePicker have to do with time entry? Well lets see …
The DateTimePicker supports different types of masks:
- long (Thursday, June 07, 2007)
- short (6/7/2007)
- time (12:00:00 AM)
- and custom
The custom mask is what interests us, because what I want to achieve is something like this:
This is easily done by doing the following settings to the DateTimePicker:
this.dateTimePicker1.CustomFormat = “HH:mm”;
this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePicker1.Location = new System.Drawing.Point(34, 33);
this.dateTimePicker1.MaxDate = new System.DateTime(2009, 7, 15, 0, 0, 0, 0);
this.dateTimePicker1.MinDate = new System.DateTime(2007, 6, 7, 21, 45, 0, 0);
this.dateTimePicker1.Name = “dateTimePicker1″;
this.dateTimePicker1.ShowUpDown = true;
this.dateTimePicker1.Size = new System.Drawing.Size(53, 20);
this.dateTimePicker1.TabIndex = 0;
this.dateTimePicker1.Value = new System.DateTime(2007, 6, 7, 21, 45, 0, 0);
The end result is that we have an input control that not only can retrieve and present information to the user it does also validate the input and rejects values like 25:87 and these without having the input leave the control. This bodes well for enterprise application where most of the user input is done without ever touching the mouse. Of course the up/down buttons of the control can be used with the mouse and thus the input is not keyboard dependent.
Thanks to Nelu S. for the heads-up!
Cheers



Entries (RSS)