Changing DateTimePicker MinDateTime to something other than 01/01/1753
-
Hi, Apologies if this has been answered already, but I've done quite a lot of searching and haven't yet uncovered an answer. I can't see a good reason why the DateTimePicker is limited to dates after 01/01/01753. I'm writing some genealogy software and am having problems with this somewhat arbitrary limitation. So I've been trying to figure out if there's a way to amend this setting. Not being an expert in C# I'm not sure if there's some way I can force the static readonly field MinDateTime to something different even though the design has tried to stop me. Is it possible to do such a thing? I don't even mind if someone has a massive hack that would enable me to change this value :) I've tried many things, including using Reflection to get hold of the field and change it (it won't get hold of the field for me, just returns null). Have now reached the limits of my knowledge, so that's why I'm asking you knowledgeable people! Thanks in advance for any help or suggestions PS It's worth mentioning that I'm not after writing a completely new control for this as it's a home project, I'm not blessed with much free time and DateTimePicker is absolutely fine for my use barring this one limitation. But if anyone knows of a decent quality free control I'm willing to give it a try as an alternative.
-
Hi, Apologies if this has been answered already, but I've done quite a lot of searching and haven't yet uncovered an answer. I can't see a good reason why the DateTimePicker is limited to dates after 01/01/01753. I'm writing some genealogy software and am having problems with this somewhat arbitrary limitation. So I've been trying to figure out if there's a way to amend this setting. Not being an expert in C# I'm not sure if there's some way I can force the static readonly field MinDateTime to something different even though the design has tried to stop me. Is it possible to do such a thing? I don't even mind if someone has a massive hack that would enable me to change this value :) I've tried many things, including using Reflection to get hold of the field and change it (it won't get hold of the field for me, just returns null). Have now reached the limits of my knowledge, so that's why I'm asking you knowledgeable people! Thanks in advance for any help or suggestions PS It's worth mentioning that I'm not after writing a completely new control for this as it's a home project, I'm not blessed with much free time and DateTimePicker is absolutely fine for my use barring this one limitation. But if anyone knows of a decent quality free control I'm willing to give it a try as an alternative.
codeprojectalanr wrote:
I can't see a good reason why the DateTimePicker is limited to dates after 01/01/01753.
Take a look at the entry for January 1[^]. The problem is calculating dates in the Julian Calendar because of leap years, which is why we switched to the Gregorian version. I don't know if it will help you but there is a Julian Calendar Class[^] in the .Net Framework.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
codeprojectalanr wrote:
I can't see a good reason why the DateTimePicker is limited to dates after 01/01/01753.
Take a look at the entry for January 1[^]. The problem is calculating dates in the Julian Calendar because of leap years, which is why we switched to the Gregorian version. I don't know if it will help you but there is a Julian Calendar Class[^] in the .Net Framework.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Thanks for the reply Henry. I'm still not convinced that it's necessary to impose the fact that there are some problems converting between the calendars on the control itself - this is surely a problem that the programmer creating the application should be in control of. Despite my claims that I don't have much spare time, I've now taken the short amount of time needed to create a basic TextBox-based control that validates the date. I include the code here for anyone who might find it useful. Shame I haven't got a calendar control to accompany it, but hey, can't win everything!
public partial class DateTimeTextBox : UserControl { private DateTime m\_value = DateTime.MinValue; public DateTimeTextBox() { InitializeComponent(); } public DateTime Value { get { return m\_value; } set { m\_value = value; textBoxDate.Text = m\_value.Date.ToShortDateString(); } } private bool ValidateDate(string date, out DateTime validatedDate) { validatedDate = DateTime.MinValue; DateTimeConverter dtc = new DateTimeConverter(); try { validatedDate = (DateTime)dtc.ConvertFromString(date); } catch { return false; } return true; } private void textBoxDate\_Leave(object sender, EventArgs e) { TextBox textbox = (TextBox)sender; if (textbox != null) { bool validDate = ValidateDate(textbox.Text, out m\_value); if (!validDate) { MessageBox.Show("Invalid date"); textbox.Focus(); } } } }
-
Thanks for the reply Henry. I'm still not convinced that it's necessary to impose the fact that there are some problems converting between the calendars on the control itself - this is surely a problem that the programmer creating the application should be in control of. Despite my claims that I don't have much spare time, I've now taken the short amount of time needed to create a basic TextBox-based control that validates the date. I include the code here for anyone who might find it useful. Shame I haven't got a calendar control to accompany it, but hey, can't win everything!
public partial class DateTimeTextBox : UserControl { private DateTime m\_value = DateTime.MinValue; public DateTimeTextBox() { InitializeComponent(); } public DateTime Value { get { return m\_value; } set { m\_value = value; textBoxDate.Text = m\_value.Date.ToShortDateString(); } } private bool ValidateDate(string date, out DateTime validatedDate) { validatedDate = DateTime.MinValue; DateTimeConverter dtc = new DateTimeConverter(); try { validatedDate = (DateTime)dtc.ConvertFromString(date); } catch { return false; } return true; } private void textBoxDate\_Leave(object sender, EventArgs e) { TextBox textbox = (TextBox)sender; if (textbox != null) { bool validDate = ValidateDate(textbox.Text, out m\_value); if (!validDate) { MessageBox.Show("Invalid date"); textbox.Focus(); } } } }
Thanks for posting your code! It's a reasonable effort, I suppose. But it doesn't cope with BC Dates, so I can't check my Birthday. :laugh: :laugh:
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Thanks for the reply Henry. I'm still not convinced that it's necessary to impose the fact that there are some problems converting between the calendars on the control itself - this is surely a problem that the programmer creating the application should be in control of. Despite my claims that I don't have much spare time, I've now taken the short amount of time needed to create a basic TextBox-based control that validates the date. I include the code here for anyone who might find it useful. Shame I haven't got a calendar control to accompany it, but hey, can't win everything!
public partial class DateTimeTextBox : UserControl { private DateTime m\_value = DateTime.MinValue; public DateTimeTextBox() { InitializeComponent(); } public DateTime Value { get { return m\_value; } set { m\_value = value; textBoxDate.Text = m\_value.Date.ToShortDateString(); } } private bool ValidateDate(string date, out DateTime validatedDate) { validatedDate = DateTime.MinValue; DateTimeConverter dtc = new DateTimeConverter(); try { validatedDate = (DateTime)dtc.ConvertFromString(date); } catch { return false; } return true; } private void textBoxDate\_Leave(object sender, EventArgs e) { TextBox textbox = (TextBox)sender; if (textbox != null) { bool validDate = ValidateDate(textbox.Text, out m\_value); if (!validDate) { MessageBox.Show("Invalid date"); textbox.Focus(); } } } }
The date conversion is tied to the control because it has to know what calendar you are using in order to display the correct calendar for any date before 1753... Having said that, I don't see why the control's designers didn't just allow us to set a simple isJulian bool that would tell the control that the calendar is (or is not) using the Julian/Gregorian system. Then it could handle any date and it would leave it to the user to know when and when not to use the Julian calendar. If you ask me the designers at Microsoft just chose to take the easy (lazy) way out.