Reputation: 8538
I am developing a winform
based Desktop application in C#. I would like the user to set the DateTimePicker to Null
. I am developing a search box, and would like to ignore the date if it is set to Null
Here is what I am doing :
this.dateTimePicker2.CustomFormat = " ";
this.dateTimePicker2.Format = DateTimePickerFormat.Custom;
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
this.dateTimePicker2.CustomFormat = "dd-MM-yy";
}
So far so good. However, once the user selects the date, the dateTimePicker2
control shows some date ( the date the user has selected). There is no way to set the date to null again. I am not keen to enable the checkbox associated with the datetimepicker
control.
I was wondering if it is possible to set the datetimepicker
date to null
.
Thanks
Upvotes: 4
Views: 47425
Reputation: 5
This implementation replaces the original Value property with a nullable DateTime which handles the CheckBox state in the setter. The default date can be set flexibly, in my circumstance a start of year date is easier to work with then min value or today. Setting a custom format of " " when null i felt doesn't improve to user experience but feel free to change, I chose to keep it simple.
public class DateTimePickerAdvanced : DateTimePicker
{
public DateTime DefaultDate { get; set; }
public DateTimePickerAdvanced()
{
DefaultDate = new DateTime(2020, 01, 01);
}
public new DateTime? Value
{
get
{
return this.ShowCheckBox && !this.Checked ? null : (DateTime?)base.Value;
}
set
{
if (value == null && !this.ShowCheckBox)
{
throw new Exception("ShowCheckBox must be enabled in order to set DateTimePicker to null");
}
base.Value = value == null ? DefaultDate : (DateTime)value;
if (this.ShowCheckBox)
{
if (value == null && this.Checked)
{
this.Checked = false;
}
else if (value != null && !this.Checked)
{
this.Checked = true;
}
}
}
}
}
Upvotes: 0
Reputation: 51
Add checkbox
to your datetime
picker property:
string datevalue = dateTimePicker2.Checked != true ? "N/A": dateTimePicker2.Text;**
Upvotes: 4
Reputation: 547
This worked for me when using a DateTimeControl on a SharePoint Application Page.
if (dtcOpenDate.SelectedDate != DateTime.Now)
{
item["Open Date"] = dtcOpenDate.SelectedDate;
}
Upvotes: 0
Reputation: 1172
DateTime is not nullable type.
But you can do this trick:
Just add a checkbox on the date picker, you can find it on properties and set ShowCheckBox to True.
You can use this conditional:
if(datePicker.Checked){
//Do some stuff to ignore the Date Picker value
}
For additional info, I'm use Microsoft Visual Studio 2010, I'm not check the other IDE yet.
Hope this help.
Cheers...
Upvotes: 0
Reputation: 37770
I was wondering if it is possible to set the datetimepicker date to null
There's no way to set DateTimePicker.Value
to null
, because its type isn't nullable. MSDN says, that:
If the Value property has not been changed in code or by the user, it is set to the current date and time (DateTime.Now).
But this isn't a problem. You should set to null some bound property, not the DateTimePicker.Value
:
public class MyModel : INotifyPropertyChanged
{
public DateTime? DateTimeFilter
{
get { return dateTimeFilter; }
set
{
if (dateTimeFilter != value)
{
dateTimeFilter = value;
OnPropertyChanged("DateTimeFilter");
}
}
}
private DateTime? dateTimeFilter;
// INotifyPropertyChanged implementation is omitted
}
public partial class Form1 : Form
{
private readonly MyModel model;
public Form1()
{
InitializeComponent();
model = new MyModel();
dateTimePicker1.DataBindings.Add("Value", model, "DateTimeFilter", true, DataSourceUpdateMode.OnPropertyChanged, DateTime.Now);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(model.DateTimeFilter.HasValue ? string.Format("User has selected '{0}'.", model.DateTimeFilter) : "User hasn't selected anything.");
}
private void button2_Click(object sender, EventArgs e)
{
// here's the data binding magic: our model's property becomes null,
// and datetimepicker's value becomes DateTime.Now, as it was initially set
model.DateTimeFilter = null;
}
}
Upvotes: 1
Reputation: 18853
This is in reference from a post that is old but other users on this site have posted it here you go
// Use ValueChanged to decide if the value should be displayed:
dateTimePicker1.ValueChanged += (s, e) => { dateTimePicker1.CustomFormat = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? "MM/dd/yyyy" : " "; };
//When getting the value back out, use something like the following:
DateTime? dt = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? (DateTime?) dateTimePicker1.Value : null;
// or
DateTime dt2 = (dateTimePicker1.Checked && dateTimePicker1.Value != dateTimePicker1.MinDate) ? dateTimePicker1.Value : DateTime.MinValue;
or you can set the CustomFormat to " " an empty space like the following below
dateTimePicker1.CustomFormat= " ";
Upvotes: 5
Reputation: 43616
I dont think its possible because DateTime is not nullable.
But you could use DateTime.MinValue
this way tou can still compare easily
if (datetimepicker.DateTime == DateTime.MinValue)
{
// just as good as null, maybe
}
Upvotes: 1