470,594 Members | 1,101 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Problem with ValueChanged event of DateTimePicker

Hi All,
I am trying to write a validation rule for DateTimePicker, which will
prevent user from selecting saturday or sunday as a value. For this I
have added an event handler with following code:

private void dtpStartDate_ValueChanged(object sender, System.EventArgs
e)
{
System.DayOfWeek i = dtpStartDate.Value.DayOfWeek;
if((i == System.DayOfWeek.Sunday) || (i == System.DayOfWeek.Saturday))
{
MessageBox.Show("Please try again, can't select a weekend day");
//Second call to ValueChanged
this.dtpStartDate.Value = DateTime.Today;
}
return;
}

If you see the code flow, the eventhandler should get called 2 times,
first with the date value chosen by then end user and second with
Today's date which I am setting inside my code. No problem here.

But the problem is, the ValueChanged function is getting called again
after completing above two calls. And surprisingly with the same value
that user has choose on the form.

This is the reason, the message box is shown twice to the end user. I
can prevent the message box getting displayed twice by having a flag.
But I am more interested in knowing why the event handler delegate is
behaving incorrectly. Is this a BUG or I am doing anything wrong?

Sep 7 '06 #1
1 7104
Got the solution :)...........
declare a form member as below:
private DateTime m_CurrentDate;

and here is the modified ValueChanged:
private void dtpStartDate_ValueChanged(object sender, System.EventArgs
e)
{
bool bComp = false;
System.DayOfWeek i = this.dtpStartDate.Value.DayOfWeek;
if((i == System.DayOfWeek.Sunday) || (i == System.DayOfWeek.Saturday))
{
DateTime dt = ((DateTimePicker)sender).Value;
if((dt.Day == this.m_CurrentDate.Day) && (dt.Month ==
this.m_CurrentDate.Month) && (dt.Year == this.m_CurrentDate.Year))
bComp = true;

if(bComp == false)
{
this.m_CurrentDate = ((DateTimePicker)sender).Value;
MessageBox.Show("Can't select a weekend day, date will be reset to
today's date.");
}
this.dtpStartDate.Value = DateTime.Today;
}
return;
}
Regards,
-Sac

sk*******@gmail.com wrote:
Hi All,
I am trying to write a validation rule for DateTimePicker, which will
prevent user from selecting saturday or sunday as a value. For this I
have added an event handler with following code:

private void dtpStartDate_ValueChanged(object sender, System.EventArgs
e)
{
System.DayOfWeek i = dtpStartDate.Value.DayOfWeek;
if((i == System.DayOfWeek.Sunday) || (i == System.DayOfWeek.Saturday))
{
MessageBox.Show("Please try again, can't select a weekend day");
//Second call to ValueChanged
this.dtpStartDate.Value = DateTime.Today;
}
return;
}

If you see the code flow, the eventhandler should get called 2 times,
first with the date value chosen by then end user and second with
Today's date which I am setting inside my code. No problem here.

But the problem is, the ValueChanged function is getting called again
after completing above two calls. And surprisingly with the same value
that user has choose on the form.

This is the reason, the message box is shown twice to the end user. I
can prevent the message box getting displayed twice by having a flag.
But I am more interested in knowing why the event handler delegate is
behaving incorrectly. Is this a BUG or I am doing anything wrong?
Sep 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Billy Jacobs | last post: by
6 posts views Thread by Joanna Carter \(TeamB\) | last post: by
4 posts views Thread by steve bull | last post: by
2 posts views Thread by Bill Nguyen | last post: by
43 posts views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.