monthCalendar DateChanged issue - documented behaviour?

  • Thread starter Thread starter jeff
  • Start date Start date
J

jeff

I've searched for this problem but haven't found any posts on it - hope this
isn't a duplicate...

I had a strange event occur earlier today and have isolated the 'problem'.
In its simplest form I have a monthCalendar control and textBox control on a
form, and have added an event handler that clears the text in the textBox
when the DateChanged event is fired. The problem is that I find if text is
entered in the textBox, after just under 2 minutes (on my system) the
DateChanged event is apparently fired (even if no activity is occurring) and
the textBox will be cleared.

To isolate this I went back to the DateChanged event handler example in the
MS docs and stripped the code down (also changing some of the values for
sizes and alignments). Here's what I have:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MonthCalendar monthCalendar1;
private System.Windows.Forms.TextBox textBox1;

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

public Form1()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox1.Location = new System.Drawing.Point(248, 16);
this.textBox1.Multiline = true;
this.textBox1.Size = new System.Drawing.Size(224, 32);

// Create the calendar.
this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();

// Set the calendar location.
this.monthCalendar1.Location = new System.Drawing.Point(47, 16);

this.monthCalendar1.DateChanged += new
System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged);

// Set up how the form should be displayed and add the controls to
the form.
this.ClientSize = new System.Drawing.Size(500, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[]
{this.textBox1, this.monthCalendar1});
this.Text = "Month Calendar Example";
}

private void monthCalendar1_DateChanged(object sender,
System.Windows.Forms.DateRangeEventArgs e)
{
// If the date changes, clear the text box
this.textBox1.Text = "";
}
}

Is this behaviour documented anywhere?

I assume that I'm merely using the wrong "tool" and should be monitoring a
different event or clearing the textbox differently, but this still seems
very odd to me.

I initially set this up with Visual Studio 2005 Beta 2 but tested the above
in SharpDevelop.

Any insight would be appreciated.
 
Hi

I think this is by design.
The MonthCalendar control simply forwards the MCN_SELCHANGE notification it
gets from OS.

The MCN_SELCHANGE notification is being sent by the Month Calendar control
every two minutes to ensure that the calendar is automatically updated in
the event that
the date should change while the control is being displayed.

However, we has a simple workaround for the application: in the
DateChangedEvent, see if e.Start and e.End point to the same day. It they
do, then the date did not really change.

Hope this helps.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Peter Huang" said:
Hi

I think this is by design.
The MonthCalendar control simply forwards the MCN_SELCHANGE notification
it
gets from OS.

The MCN_SELCHANGE notification is being sent by the Month Calendar control
every two minutes to ensure that the calendar is automatically updated in
the event that
the date should change while the control is being displayed.

However, we has a simple workaround for the application: in the
DateChangedEvent, see if e.Start and e.End point to the same day. It they
do, then the date did not really change.

Hope this helps.

This does. In fact it explains another piece that I didn't mention. In my
real application the calendar's displayed in a pane that can be hidden by
the user. I found that if the calendar control wasn't displayed, the problem
didn't occur.

I am not sure I like that an event called "DateChanged" can be fired when
the date didn't change, but that's a topic for another thread I think... :-)

Thanks for the help.
 
Hi

I am glad that my suggestion helps you.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top