473,382 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 7556
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Billy Jacobs | last post by:
I have created a DataGridColumnDatePicker Component so that I can put a datetimepicker control in my datagrid. It almost works. When I put my mouse in the cell it changes to a datetimepicker...
21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
6
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T...
4
by: steve bull | last post by:
I have created a UserControl and added it to my toolbox. It appears in the toolbox with the icon I created for it. The problem is that when I drag the control onto a form no code is generated. If I...
2
by: Javaman59 | last post by:
I want to add a ValueChanged event handler to a control. If I add the handler during form construction then the ValueChanged handler gets called when the control is initialized, and I don't want...
2
by: Bill Nguyen | last post by:
Every time I jumped to a new month, the app kept popping up the message box for subsequent months until 2050! If I stayed within the same month (current month), it worked fine. Please take a look...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
2
by: Steven | last post by:
Hello, I want to raise the ValueChanged event ONLY when i move the track bar manually, but not the value changed, how to do in the event below? thank you Private Sub TrackBar1_ValueChanged(ByVal...
0
by: priyamtheone | last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have: 1) Initially all the cells of the dtp column should be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.