4

In the process of migrating one of my Win32 VCL application from Delphi 2006 to delphi XE6 I encountered the following issue :

One of my forms has a TMonthCalendar (plugged on a TPanel for the record) to help the user select a week to view in a graph. By week I mean Monday being the first day and Sunday the last (french locale). To achieve such a week selection pattern I set the multiSelect property to true and put the following code inside the CalendarClick event :

MonthCalendar1.MultiSelect := True;
//Temporarily storing the selected day in a variable
TempoDate := MonthCalendar1.Date;

//searching for the monday right before the selected day (by user)
while dayOfWeek(TempoDate) <> 2 do
  TempoDate := IncDay( TempoDate , -1 );

//Setting the monday as the start date of the selection
MonthCalendar1.Date := TempoDate;
//Setting the Sunday as the last day of selection
MonthCalendar1.EndDate := IncDay(tempoDate, 6);

That used to work well on Delphi 2006 ( compiled on a win XP computer ). Now that I have ported the same code to Delphi XE6 ( compiled on a win7 computer ) I have the following problems :

  • When clicking the right arrow (>) to switch to the next month it fails most of the time. It actually fails when the monday of the week containing the 1st of the next month is still in the previous month. Ex : switching from Sept '14 to Oct '14 fails because the monday before Oct. 1st is in september (Monday Sept. 29th). So that brings me back to September. On the other hand, switching from August 14 to September 14 works because Sept. 1st is a monday.
  • When clicking on the first days of the next month (the few grey one you can click on) the month doesn't switch anymore.

all that used to work before.

I've made some specific isolation tests :

  1. Creating a minimal app under XE6 with the same behaviour -> still fails (of course)
  2. Creating the same minimal app under Delphi 2006 -> it all work as expected.

My intuition is that the TMonthCalendar now takes the .Date property to define which month to show, while on D2006 it used to take .EndDate property. Doesn't know if this is a VCL evolution or a microsoft MonthCalendar underlying component behaviour change (since i compiled on XP then SEVEN ).

Thanks for your help

Useful documentation :

http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.ComCtrls.TMonthCalendar http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar(v=vs.110).aspx

mathieu
  • 235
  • 2
  • 11
  • Should be VCL, I think, no problem with a XE2 compile on W7. BTW, your code, in fact, prevents from switching from Sept '14 to Oct '14, as when I select Sept 14, code mutates the date to be Sept 8, and then I can only switch to Oct 8. – Sertac Akyuz Sep 03 '14 at 16:32
  • So you tried the same minimal app thing with XE2 VCL on windows 7 and it worked? That would definitely points to a VCL change between XE2 and XE6. – mathieu Sep 04 '14 at 09:17

1 Answers1

1

Unfortunately I can't provide you with solution but athleast I have eplanation for current behavior.

The problem you are facing is the TMonthCalender controll itself and which date fields is trated as selected even when using multiselect.
If you take a good look you will notice that even when using multiselect one day always have doted square around it. That date controls which month is focused.
So now you need to figure out how to change that behavior working with multiple selection enabled. I laredy tried setting the Date and EndDate properties so that EndDate value was actually lower since I thought that doing so miygt force MonthCalendar to treat last day of the weak to be selected when detirmining which month is focused but it has no effect. But it has no effect.

As for finding starting and ending week date use these functions:

TempDate := MonthCalendar1.Date;
WeekStart := StartOfTheWeek(TempDate);
WeekEnd := EndOfTheWeek(TempDate);

Both of these functions treat monday as fist day of the week.

I wish I could have helped you more.

SilverWarior
  • 7,372
  • 2
  • 16
  • 22