Last active
August 29, 2015 13:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dateTimePickerToClear.Format = DateTimePickerFormat.Custom; | |
dateTimePickerToClear.CustomFormat = " "; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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