MonthCalendar DateChanged event problem

D

DS

I seem to have stumbled upon something that I can't explain and was
wondering if someone else could maybe help me figure out why this
happens. I've tried it in both VS2003 and with VS2005b2 and in both
cases the result is the same: an ArgumentOutOfRangeException.

Add the following to a blank WindowsForm in C# then call doStuff() from
the constructor or a buttonClick event. If you click on any date, it
pops up a messageBox (as it should) telling you the new date. Great. Now
try clicking one of the buttons to goto the next or previous month. At
first one messageBox shows up telling you the selection has changed to
the first of the next (or previous month), then when you click ok, it
seems to go haywire. It flips through about 19 months before crashing.
This is especially weird since it should only flip 1 month
(scrollChange=1). Changing the scrollChange has no effect, it just flips
through about 19 iterations of whatever scrollChange is set to. Ideas?

Thanks in advance,
-DS
---- Code below ----

private void doStuff()
{
MonthCalendar mc = new MonthCalendar();
mc.CalendarDimensions = new Size(1,1);
mc.Location = new Point(50, 50);
mc.MaxSelectionCount = 1;
mc.SetDate(DateTime.Parse("1 January 2005"));
mc.ScrollChange = 1;
this.Controls.Add(mc);
mc.DateChanged += new DateRangeEventHandler(mc_DateChanged);
}

void mc_DateChanged(object sender, DateRangeEventArgs e)
{
MessageBox.Show("date changed " + e.Start.ToString());
}
 
D

DS

DS said:
I seem to have stumbled upon something that I can't explain and was
wondering if someone else could maybe help me figure out why this
happens. I've tried it in both VS2003 and with VS2005b2 and in both
cases the result is the same: an ArgumentOutOfRangeException.

Add the following to a blank WindowsForm in C# then call doStuff() from
the constructor or a buttonClick event. If you click on any date, it
pops up a messageBox (as it should) telling you the new date. Great. Now
try clicking one of the buttons to goto the next or previous month. At
first one messageBox shows up telling you the selection has changed to
the first of the next (or previous month), then when you click ok, it
seems to go haywire. It flips through about 19 months before crashing.
This is especially weird since it should only flip 1 month
(scrollChange=1). Changing the scrollChange has no effect, it just flips
through about 19 iterations of whatever scrollChange is set to. Ideas?

Thanks in advance,
-DS
---- Code below ----

private void doStuff()
{
MonthCalendar mc = new MonthCalendar();
mc.CalendarDimensions = new Size(1,1);
mc.Location = new Point(50, 50);
mc.MaxSelectionCount = 1;
mc.SetDate(DateTime.Parse("1 January 2005"));
mc.ScrollChange = 1;
this.Controls.Add(mc);
mc.DateChanged += new DateRangeEventHandler(mc_DateChanged);
}

void mc_DateChanged(object sender, DateRangeEventArgs e)
{
MessageBox.Show("date changed " + e.Start.ToString());
}

I forgot to add that I believe I need to use the DateChanged event as
opposed to the DateSelected because I need to get keyboard changes as
well. Unfortunately DateSelected is only fired by mouse clicks whereas
DateChanged fires from both.
 
T

Truong Hong Thi

It sounds like a bug of .NET. It does not happen if you do not show a
modal dialog in the event handler - in that case the event is fired
just twice!

That problem only happens if you change months by clicking one of the
arrow buttons. In other cases, the event is fired at most twice.
 
D

DS

Truong said:
It sounds like a bug of .NET. It does not happen if you do not show a
modal dialog in the event handler - in that case the event is fired
just twice!

That problem only happens if you change months by clicking one of the
arrow buttons. In other cases, the event is fired at most twice.

Hi Truong, thanks fro the feedback. Its nice to know that someone else
ran into a similar problem. I tried wiring it up to a multiline textbox
and had mixed results. If there was no '\r\n' in the string it would
crash, yet if it had the linefeed/cr after the date it didn't crash. Not
reliable enough for my liking.

Another problem that others seem to have run into with the DateChanged
event is that every minute or so it seems to fire (probably because of
the system clock or something), and this is just plain bad. I think I'll
just forgo DateChanged and use DateSelected instead-- it seems to have
less baggage. Only thing is that keyboard support is nixed since it only
fires on a mouse click event.

-DS
 

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