DateTimePicker MinDate Bug

N

Nitin Koshy

/*Code Start*/
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private DateTimePicker dateTimePicker1;
private DateTimePicker dateTimePicker2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.dateTimePicker1 = new
System.Windows.Forms.DateTimePicker();
this.dateTimePicker2 = new
System.Windows.Forms.DateTimePicker();
this.SuspendLayout();
//
// dateTimePicker1
//
this.dateTimePicker1.CustomFormat = "hh:mm tt";
this.dateTimePicker1.Format =
System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePicker1.Location = new System.Drawing.Point(40,
56);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.ShowUpDown = true;
this.dateTimePicker1.Size = new System.Drawing.Size(136, 20);
this.dateTimePicker1.TabIndex = 0;
this.dateTimePicker1.ValueChanged += new
System.EventHandler(this.dateTimePicker1_ValueChanged);
//
// dateTimePicker2
//
this.dateTimePicker2.CustomFormat = "hh:mm tt";
this.dateTimePicker2.Format =
System.Windows.Forms.DateTimePickerFormat.Custom;
this.dateTimePicker2.Location = new System.Drawing.Point(40,
88);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.ShowUpDown = true;
this.dateTimePicker2.Size = new System.Drawing.Size(136, 20);
this.dateTimePicker2.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.dateTimePicker1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
dateTimePicker1.MinDate = DateTime.Now;
dateTimePicker1.MaxDate = DateTime.Now.AddHours(2);

dateTimePicker2.MinDate = dateTimePicker1.MinDate;
dateTimePicker2.MaxDate = dateTimePicker1.MaxDate;
}

private void dateTimePicker1_ValueChanged(object sender,
System.EventArgs e)
{
dateTimePicker2.Value = dateTimePicker1.Value;
/*
* why do I get a System.ArgumentException here when I
decrement the value of dateTimePicker1
using the UpDown keys ?

Additional Info says : '16/05/2004 15:35:43' is not a valid
value for 'Value'.
'Value' should be between 'MinDate'
and 'MaxDate'
*/
}
}
}
/*Code End*/

Anyone got any clues to why I get the ArgumentException at
dateTimePicker1_ValueChanged please? Bug ?


Cheers,
Nitin Koshy
 
G

Guest

Whether or not it's a bug depends on your definition of bug, I guess. I would probably vote for yes

The reason you get the exception is that DateTime has is accurate down to the tick, but DateTimePicker's resolution only goes to the second. Therefore, when you increment the value of the DateTimePicker with the UI and decrement it again, you're trying to set its value to a DateTime that's anywhere from 1 to 9,999,999 ticks ealier than your MinDate

Here's the simplest work around I could figure

private void Form1_Load(object sender, System.EventArgs e

DateTime min = DateTime.Now

TimeSpan time = new TimeSpan
min.TimeOfDay.Days,
min.TimeOfDay.Hours,
min.TimeOfDay.Minutes,
min.TimeOfDay.Seconds,
0)

min = DateTime.Today.Add(time)

dateTimePicker1.MinDate = min
dateTimePicker1.MaxDate = min.AddHours(2)

dateTimePicker2.MinDate = dateTimePicker1.MinDate
dateTimePicker2.MaxDate = dateTimePicker1.MaxDate


Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top