Help with MonthCalendar class
-
Could somebody give me a smidge of help with the System.Windows.Forms.MonthCalendar class? Or at least point me to some good tutorials using it. All I'm trying to do at this point is this: After a user has selected some dates in it, I'd like to go through the entire range of dates (or at least a reasonable portion) and get all selected dates that the user selected. I want to store those dates into an array of DateTime objects. Any help or pointers? Thanks.
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
Could somebody give me a smidge of help with the System.Windows.Forms.MonthCalendar class? Or at least point me to some good tutorials using it. All I'm trying to do at this point is this: After a user has selected some dates in it, I'd like to go through the entire range of dates (or at least a reasonable portion) and get all selected dates that the user selected. I want to store those dates into an array of DateTime objects. Any help or pointers? Thanks.
There are only 10 types of people in this world....those that understand binary, and those that do not.
You can use the
SelectionRange
(or theSelectionStart
andSelectionEnd
properties) to iterate between the start and end dates. You cold either temporarily add these to a growableArrayList
, or calculate the number of elements aDateTime[]
array would require. The following uses the latter method:TimeSpan span = monthCal.SelectionEnd - monthCal.SelectionStart;
int count = span.Days + 1; // Subtraction of days requires that you add 1.
DateTime[] dts = new DateTime[count];
for (int i=0; i<count; i++)
dts[i] = monthCal.SelectionStart + new TimeSpan(1, 0, 0, 0);-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
You can use the
SelectionRange
(or theSelectionStart
andSelectionEnd
properties) to iterate between the start and end dates. You cold either temporarily add these to a growableArrayList
, or calculate the number of elements aDateTime[]
array would require. The following uses the latter method:TimeSpan span = monthCal.SelectionEnd - monthCal.SelectionStart;
int count = span.Days + 1; // Subtraction of days requires that you add 1.
DateTime[] dts = new DateTime[count];
for (int i=0; i<count; i++)
dts[i] = monthCal.SelectionStart + new TimeSpan(1, 0, 0, 0);-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Thanks for the reply. That looks fairly straightforward. I'm not familiar with the TimeSpan class (yet), but it looks relatively straightwforward. But this allows me to traverse it....is there anything in that TimeSpan class that lets me see whether the date in question is selected (highlighted in the control) or not?
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
Thanks for the reply. That looks fairly straightforward. I'm not familiar with the TimeSpan class (yet), but it looks relatively straightwforward. But this allows me to traverse it....is there anything in that TimeSpan class that lets me see whether the date in question is selected (highlighted in the control) or not?
There are only 10 types of people in this world....those that understand binary, and those that do not.
The
TimeSpan
structure - not class (read the SDK documentation for more details) - just represents a difference in relation to aDateTime
. TheMonthCalendar
class only exposes aSelectionStart
andSelectionEnd
date (theSelectionRange
only contains these two properties as well). TheTimeSpan
has nothing to do with this. See theMonthCalendar
documentation in the .NET Framework SDK for more details.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
The
TimeSpan
structure - not class (read the SDK documentation for more details) - just represents a difference in relation to aDateTime
. TheMonthCalendar
class only exposes aSelectionStart
andSelectionEnd
date (theSelectionRange
only contains these two properties as well). TheTimeSpan
has nothing to do with this. See theMonthCalendar
documentation in the .NET Framework SDK for more details.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Yes, I see how I could use that to select a certain day, but how from there can I tell if that day has been selected/highlighted by the user? I've gone through documentation and this is my primary problem. Thanks though. :)
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
Yes, I see how I could use that to select a certain day, but how from there can I tell if that day has been selected/highlighted by the user? I've gone through documentation and this is my primary problem. Thanks though. :)
There are only 10 types of people in this world....those that understand binary, and those that do not.
As I keep saying, you must iterate through the dates between the
MonthCalendar.SelectionStart
andMonthCalendar.SelectionEnd
dates. That snippet of code I posted the first time does this. TheTimeSpan
merely addsi
days to theSelectionState
DateTime
and adds that to the array. TheSelectionRange
is contiquous to simply iterating inclusively between theSelectionStart
andSelectionEnd
dates is how you get an array of the selected dates.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
As I keep saying, you must iterate through the dates between the
MonthCalendar.SelectionStart
andMonthCalendar.SelectionEnd
dates. That snippet of code I posted the first time does this. TheTimeSpan
merely addsi
days to theSelectionState
DateTime
and adds that to the array. TheSelectionRange
is contiquous to simply iterating inclusively between theSelectionStart
andSelectionEnd
dates is how you get an array of the selected dates.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
How exactly, then, is it determining if the date was selected/highlighted? That snipped just appears to go through all the dates. Does the SelectionRange only give dates that were selected? BTW....I'm at work now and unable to play with the code in question ("my own" code at home)....so I'm going by memory (e.g. code is not in front of me). That's why I'm unable to put this in and test and see what you mean.
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
How exactly, then, is it determining if the date was selected/highlighted? That snipped just appears to go through all the dates. Does the SelectionRange only give dates that were selected? BTW....I'm at work now and unable to play with the code in question ("my own" code at home)....so I'm going by memory (e.g. code is not in front of me). That's why I'm unable to put this in and test and see what you mean.
There are only 10 types of people in this world....those that understand binary, and those that do not.
As I've mentioned a couple times now, the
SelectionRange
is inclusive, meaning that every date betweenMonthCalendar.SelectionStart
andMonthCalendar.SelectionEnd
is selected. Unless there's some undocumented feature of theMonthCalendar
or you've extended and overridden its behavior, this is how theMonthCalendar
works - a single date range can be selected. Now, if you want to track every selection the user makes, handle theMonthCalendar.DateSelected
event and add the range fromDateRangeEventArgs.Start
andDateRangeEventArgs.End
to anArrayList
or something, which you can later useArrayList.CopyTo
to create an array from the list.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
As I've mentioned a couple times now, the
SelectionRange
is inclusive, meaning that every date betweenMonthCalendar.SelectionStart
andMonthCalendar.SelectionEnd
is selected. Unless there's some undocumented feature of theMonthCalendar
or you've extended and overridden its behavior, this is how theMonthCalendar
works - a single date range can be selected. Now, if you want to track every selection the user makes, handle theMonthCalendar.DateSelected
event and add the range fromDateRangeEventArgs.Start
andDateRangeEventArgs.End
to anArrayList
or something, which you can later useArrayList.CopyTo
to create an array from the list.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
I've got it now and am only posting it here now for posterity. I must have been pretty tired when I worked on it last because it was right there as obvious as could be. I swear I went through the docs on the MonthCalendar class about 5 times and didn't see what I wanted, but NOW I see there is a property BoldedDates which returns to me an array of DateTime[] of all the dates that are bolded. Which is EXACTLY what I was trying to get. Why it was so hard, I don't know. Like I said...I must have been tired...it was late last Saturday or Sunday night when I played with it last.
There are only 10 types of people in this world....those that understand binary, and those that do not.
-
I've got it now and am only posting it here now for posterity. I must have been pretty tired when I worked on it last because it was right there as obvious as could be. I swear I went through the docs on the MonthCalendar class about 5 times and didn't see what I wanted, but NOW I see there is a property BoldedDates which returns to me an array of DateTime[] of all the dates that are bolded. Which is EXACTLY what I was trying to get. Why it was so hard, I don't know. Like I said...I must have been tired...it was late last Saturday or Sunday night when I played with it last.
There are only 10 types of people in this world....those that understand binary, and those that do not.
You never said bolded dates - those are different from selected dates, hence even the different property names. If you had specified that before, I could've told you immediately; of course, if you had looked at the class members like I mentioned you should do before - i.e., read the documentation - it should'be been obvious. There is a big difference between bolded and selected dates, though.
-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----