MonthCalendar display problem

D

DS

I seem to have run into another strange anomaly with the monthCalendar
control. I want to display 6 months at time with either the first half
of the year (January-June) or the later half of the year (July-Dec)
being rendered- depending on where todays date falls. I also want
today's date to be selected. This poses a problem. Refer to the code below.

Add a call to createCalendar() to your constructor (AFTER the
InitializeComponents() call). If you leave it as is without calling
selectToday(), it displays the correct 6-month block starting with the
correct month. If however you uncomment selectToday(), it displays
Nov-April, which is NOT what I want, so comment it back again.

Next try adding a button to the form, and in the onClick event, add a
call to selectToday(), and this works perfectly-- the correct 6-month
block is displayed (July-Dec), and today is selected.

Another option is to uncomment selectToday() from createCalendar(), but
remove createCalendar() from the constructor and add the call to the
button onClick event instead. Once again this works perfectly.

So in both cases it works if its called from a button click, but not
from the constructor. Unfortunately this isn't practical because I need
it to show without a person hitting a button. Instead of calling it from
a button onClick, add it to a backGroundWorker process and it also works
correctly, but its not right to futz around with controls in another
thread. It just seems to mess up when its called before the form is
displayed or something strange like that. Whats going on here?

Thanks in advance,
-DS

-- code below --
MonthCalendar mc;
private void createCalendar()
{
mc = new MonthCalendar();
mc.CalendarDimensions = new Size(3,2);
mc.Location = new Point(0, 0);
mc.MaxSelectionCount = 1;
mc.ScrollChange = 1;
if (DateTime.Today.Month <= 6)
mc.SetDate(DateTime.Parse("1 January " + DateTime.Today.Year.ToString()));
else
mc.SetDate(DateTime.Parse("1 July " + DateTime.Today.Year.ToString()));
this.Controls.Add(mc);
//selectToday();
}

private void selectToday()
{
mc.SetDate(DateTime.Today);
}
 
D

DS

DS said:
I seem to have run into another strange anomaly with the monthCalendar
control. I want to display 6 months at time with either the first half
of the year (January-June) or the later half of the year (July-Dec)
being rendered- depending on where todays date falls. I also want
today's date to be selected. This poses a problem. Refer to the code below.

Add a call to createCalendar() to your constructor (AFTER the
InitializeComponents() call). If you leave it as is without calling
selectToday(), it displays the correct 6-month block starting with the
correct month. If however you uncomment selectToday(), it displays
Nov-April, which is NOT what I want, so comment it back again.

Next try adding a button to the form, and in the onClick event, add a
call to selectToday(), and this works perfectly-- the correct 6-month
block is displayed (July-Dec), and today is selected.

Another option is to uncomment selectToday() from createCalendar(), but
remove createCalendar() from the constructor and add the call to the
button onClick event instead. Once again this works perfectly.

So in both cases it works if its called from a button click, but not
from the constructor. Unfortunately this isn't practical because I need
it to show without a person hitting a button. Instead of calling it from
a button onClick, add it to a backGroundWorker process and it also works
correctly, but its not right to futz around with controls in another
thread. It just seems to mess up when its called before the form is
displayed or something strange like that. Whats going on here?

Thanks in advance,
-DS

-- code below --
MonthCalendar mc;
private void createCalendar()
{
mc = new MonthCalendar();
mc.CalendarDimensions = new Size(3,2);
mc.Location = new Point(0, 0);
mc.MaxSelectionCount = 1;
mc.ScrollChange = 1;
if (DateTime.Today.Month <= 6)
mc.SetDate(DateTime.Parse("1 January " +
DateTime.Today.Year.ToString()));
else
mc.SetDate(DateTime.Parse("1 July " +
DateTime.Today.Year.ToString()));
this.Controls.Add(mc);
//selectToday();
}

private void selectToday()
{
mc.SetDate(DateTime.Today);
}

Just thought I'd post a follow up since no one answered and I'm sure
someone will eventually run into a similar problem down the road.

I ended up putting the selectToday() call into the form onLoad event and
it worked! Apparently populating some controls from the constructor can
lead to some odd behavior. To me it still seems odd that setting the
selection for the MonthCalendar in the constructor causes the calendar's
'window' to change, yet in the onLoad it just changes the selection in
that 'window' but hey, whatever works :)

-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