Introduction
This very short article will address an extremely easy way to hide the date value contained in a date time picker control.While the control contains no property that will allow you to hide the current date, nor to set the value of the control to null or an empty string, it is still possible, and even very easy to generate a date time picker control that does not display a date.This could be useful if you were building a form and did not want the date time picker to show today's date and if you wanted to force the user to select a date as a means of validating completion of a form.I did not build this into the as a custom control but it certainly would be easy enough to build one if you were so inclined.
Figure 1. Now you see it, now you don't.
Getting Started Date Time Picker Controller
The solution contains only a single project and that project contains only a single form.On the form is a single date time picker, and, placed over the text area of the date time picker control is a single text box. The textbox is used to mask the date time picker and hide the value from view until the user selects a value.
Figure 2. Solution Explorer.
The code used in the form is very trivial; the approach shown is only one way that you could go about hiding the value of the date time picker and there are plenty of other options that would work equally well.What is necessary in most cases is that the value of the date time picker be mirrored in a variable of the DateTime type.I say this is necessary because before the value is set, you need to have a default value that represents an unset date (since after all, the purpose of hiding the date is merely to force the selection of a specific date).Generally setting the default value of this variable to be the minimum date will adequate (unless your application works with dates in the 1700's).
As mentioned earlier, the form contains only a single date time picker control and that control has a textbox overlaying the text area of the date time picker.The down arrow was left exposed to allow the user to open the calendar; since there is no easy way to open the calendar programmatically it cannot immediately be tied to the click event for the textbox which would have been a little nicer but it is sufficient as is.
The form class code starts out with the standard imports, namespace declaration and class declaration.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace NoDate
{
public partial class Form1 : Form
{
private DateTime mDday = Convert.ToDateTime("06/06/1944");
public Form1()
{
InitializeComponent();
// Check if the date matches a specific value
if (mDday.Date == Convert.ToDateTime("06/05/1944").Date)
{
dateTimePicker1.Value = mDday;
txtDate.Hide();
}
}
}
}
As you can see, following the default imports, a local member variable of type DateTime is declared, named "mDday" and set to the value of June 6, 1944.This value is the default value used on the control.While I set this manually there is no reason you could not populate it from a field in a database for from an external file.
In the default constructor, I test the value of local DateTime variable "mDday" by comparing it to another date. I set the date to one day before the target date; this comparison will be used to decide whether or not to hide the date value displayed in the date time picker.If the dates matched, the value of the date time picker control would have been set to the value of "mDday" and the text box "txtDate" would have been hidden so as to permit the user to see the date time value.Since I intentionally caused a mismatch, I left the date time picker value set to "DateTime.Now" and I left the "txtDate" textbox control visible so that it would obscure the display of the date time picker control's value.
The only other code in the project is the handler for date time picker control's value changed event handler.When the user selects a value from the drop down calendar, the text box used to hide the date time picker's value will be hidden and the local variable's value will be set to match the date time picker's value.
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
// Hide the txtDate TextBox
txtDate.Hide();
// Update mDday with the new value from dateTimePicker1
mDday = dateTimePicker1.Value;
}
Summary
This article was intended to demonstrate an approach to hiding the value of a date time picker from the user until the user physically sets the value.The approach can be applied to force the user to select a date value and can easily be used to validate that a selection has been made.