By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,833 Members | 2,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,833 IT Pros & Developers. It's quick & easy.

MonthCalendar display problem

P: n/a
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);
}
Nov 25 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
DS
DS wrote:
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
Nov 29 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.