Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help with MonthCalendar class

Help with MonthCalendar class

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KingTermite
    wrote on last edited by
    #1

    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.

    H 1 Reply Last reply
    0
    • K KingTermite

      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.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You can use the SelectionRange (or the SelectionStart and SelectionEnd properties) to iterate between the start and end dates. You cold either temporarily add these to a growable ArrayList, or calculate the number of elements a DateTime[] 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-----

      K 1 Reply Last reply
      0
      • H Heath Stewart

        You can use the SelectionRange (or the SelectionStart and SelectionEnd properties) to iterate between the start and end dates. You cold either temporarily add these to a growable ArrayList, or calculate the number of elements a DateTime[] 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-----

        K Offline
        K Offline
        KingTermite
        wrote on last edited by
        #3

        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.

        H 1 Reply Last reply
        0
        • K KingTermite

          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.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          The TimeSpan structure - not class (read the SDK documentation for more details) - just represents a difference in relation to a DateTime. The MonthCalendar class only exposes a SelectionStart and SelectionEnd date (the SelectionRange only contains these two properties as well). The TimeSpan has nothing to do with this. See the MonthCalendar 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-----

          K 1 Reply Last reply
          0
          • H Heath Stewart

            The TimeSpan structure - not class (read the SDK documentation for more details) - just represents a difference in relation to a DateTime. The MonthCalendar class only exposes a SelectionStart and SelectionEnd date (the SelectionRange only contains these two properties as well). The TimeSpan has nothing to do with this. See the MonthCalendar 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-----

            K Offline
            K Offline
            KingTermite
            wrote on last edited by
            #5

            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.

            H 1 Reply Last reply
            0
            • K KingTermite

              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.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              As I keep saying, you must iterate through the dates between the MonthCalendar.SelectionStart and MonthCalendar.SelectionEnd dates. That snippet of code I posted the first time does this. The TimeSpan merely adds i days to the SelectionState DateTime and adds that to the array. The SelectionRange is contiquous to simply iterating inclusively between the SelectionStart and SelectionEnd 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-----

              K 1 Reply Last reply
              0
              • H Heath Stewart

                As I keep saying, you must iterate through the dates between the MonthCalendar.SelectionStart and MonthCalendar.SelectionEnd dates. That snippet of code I posted the first time does this. The TimeSpan merely adds i days to the SelectionState DateTime and adds that to the array. The SelectionRange is contiquous to simply iterating inclusively between the SelectionStart and SelectionEnd 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-----

                K Offline
                K Offline
                KingTermite
                wrote on last edited by
                #7

                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.

                H 1 Reply Last reply
                0
                • K KingTermite

                  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.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  As I've mentioned a couple times now, the SelectionRange is inclusive, meaning that every date between MonthCalendar.SelectionStart and MonthCalendar.SelectionEnd is selected. Unless there's some undocumented feature of the MonthCalendar or you've extended and overridden its behavior, this is how the MonthCalendar works - a single date range can be selected. Now, if you want to track every selection the user makes, handle the MonthCalendar.DateSelected event and add the range from DateRangeEventArgs.Start and DateRangeEventArgs.End to an ArrayList or something, which you can later use ArrayList.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-----

                  K 1 Reply Last reply
                  0
                  • H Heath Stewart

                    As I've mentioned a couple times now, the SelectionRange is inclusive, meaning that every date between MonthCalendar.SelectionStart and MonthCalendar.SelectionEnd is selected. Unless there's some undocumented feature of the MonthCalendar or you've extended and overridden its behavior, this is how the MonthCalendar works - a single date range can be selected. Now, if you want to track every selection the user makes, handle the MonthCalendar.DateSelected event and add the range from DateRangeEventArgs.Start and DateRangeEventArgs.End to an ArrayList or something, which you can later use ArrayList.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-----

                    K Offline
                    K Offline
                    KingTermite
                    wrote on last edited by
                    #9

                    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.

                    H 1 Reply Last reply
                    0
                    • K KingTermite

                      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.

                      H Offline
                      H Offline
                      Heath Stewart
                      wrote on last edited by
                      #10

                      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-----

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups