Skip to content

Instantly share code, notes, and snippets.

@FeniXb3
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FeniXb3/9021922 to your computer and use it in GitHub Desktop.
Save FeniXb3/9021922 to your computer and use it in GitHub Desktop.
Example of clearing displayed value of DateTimePicker in WInForms with C#. Solution found at StackOverflow - http://stackoverflow.com/a/13364582/1816426
dateTimePickerToClear.Format = DateTimePickerFormat.Custom;
dateTimePickerToClear.CustomFormat = " ";
using System;
using System.Windows.Forms;
namespace DateTimePickerExample
{
public partial class DateTimeExampleForm : Form
{
DateTimePickerFormat defaultDateTimePickerFormat = DateTimePickerFormat.Long;
public DateTimeExampleForm()
{
InitializeComponent();
dateTimePickerToClear.Format = defaultDateTimePickerFormat;
dateTimePickerToClear.CustomFormat = " ";
}
private void clearDateButton_Click(object sender, EventArgs e)
{
dateTimePickerToClear.Format = DateTimePickerFormat.Custom;
}
private void dateTimePickerToClear_ValueChanged(object sender, EventArgs e)
{
dateTimePickerToClear.Format = defaultDateTimePickerFormat;
}
private void searchButton_Click(object sender, EventArgs e)
{
// if .NET 4.0 is not available
DateTime? startDate = string.IsNullOrEmpty(dateTimePickerToClear.Text.Trim())
? null
: (DateTime?)dateTimePickerToClear.Value;
// if .NET 4.0 is available
startDate = string.IsNullOrWhiteSpace(dateTimePickerToClear.Text)
? null
: (DateTime?)dateTimePickerToClear.Value;
// justo to check
MessageBox.Show(startDate.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment