Here is what it does... 1. Create a form 2. Add a container 3. Add dropdowns and Labels 4. Add the methods and properties
* there is a property on the container named actualDate. It holds the date in a date format. The date is also displayed in a messagebox for demo purposes, and you will need to take that out for production.
* Notice - the starting year is 2000. You can change that if you desire.
* Copy the code below the FINAL COMMENTS and execute it to see how it works. The methods even handle leap year. The code creates a container
* FINAL COMMENTS - All you need to do to create this for yourself is:
1. Create a container class, add 3 combo boxes for month, day, year.
2. Add the methods on the container class.
3. Put the code in the container class methods and combo box click event.
4. Once it is complete - just drag your new class to a form.
5. Since the actual date is stored as a propery on the container, you can add multiple containers to the form. This is a nice way to handle start and ending dates.
*-- Holds the proper number of days for the combo box [January = 31, February = 28/29, March = 31, etc.] DIMENSION ardays
*-- Array to hold the Character Name of the Months [January, February, etc.] DIMENSION armonths
*-- Array to hold the years [2000, 2001, etc.] DIMENSION aryears
ADD OBJECT lblmessage AS label WITH ; AutoSize = .T., ; Caption = "Month, Day, Year", ; Left = 3, ; Top = 2, ; Name = "lblMessage"
ADD OBJECT cbomonths AS combobox WITH ; RowSourceType = 5, ; RowSource = "this.parent.arMonths", ; Height = 24, ; Left = 3, ; Top = 20, ; Style = 2, ; Width = 84, ; Name = "CboMonths"
ADD OBJECT cbodays AS combobox WITH ; RowSourceType = 5, ; RowSource = "this.parent.arDays", ; Height = 24, ; Left = 99, ; Top = 20, ; Style = 2, ; Width = 49, ; Name = "CboDays"
ADD OBJECT cboyears AS combobox WITH ; RowSourceType = 5, ; RowSource = "this.parent.arYears", ; Height = 24, ; Left = 160, ; Top = 20, ; Style = 2, ; Width = 59, ; Name = "CboYears"
*-- Check to see that the number of days displayed in the drop down is correct when the user changes the month or year. For example, if they change the month from January to April the maximum number of days will change from 31 to 30. PROCEDURE checkthedays LOCAL lnCurrentDays,lnMaxDays,lnCurrentMaxDay
* get the current day setting, example 16 lnCurrentDays = THIS.cboDays.VALUE
lnCurrentMaxDay = ALEN(THIS.arDays)
DO CASE CASE INLIST(THIS.cboMonths.VALUE,1,3,5,7,8,10,12) * all months with 31 days lnMaxDays = 31 CASE INLIST(THIS.cboMonths.VALUE,4,6,9,11) * all months with 30 days lnMaxDays = 30 OTHERWISE * February - check for leap year IF MOD(VAL(THIS.arYears[THIS.cboYears.VALUE]),4) = 0 * the remainder is divisible by 0 thus making it a leap year * this is true until the the year 2104, at which time I'll be dead. lnMaxDays = 29 ELSE lnMaxDays = 28 ENDIF ENDCASE
IF m.lnMaxDays <> m.lnCurrentMaxDay DECLARE THIS.arDays[m.lnMaxDays] FOR lni = 1 TO m.lnMaxDays THIS.arDays[lni] = PADL(ALLTRIM(STR(lni)),2,"0") ENDFOR * the month/year combination has caused a changed in the * number of days (base on the starting value) so we will need * to requery the combo box so it displays correctly. THIS.cboDays.REQUERY ENDIF
IF m.lnCurrentDays > m.lnMaxDays * example: * this would occur if we were sitting on Jan 31 and then changed * the month to April. This will changed the day to 30 for us. THIS.cboDays.VALUE = m.lnMaxDays
* otherwise a blank space will exist at the bottom of the combo-dropdown * if the array is smaller (again, going from 31 to 30 days). ENDIF
PROCEDURE makeactualdate LOCAL lcDay,lcMonth,lcYear lcDay = THIS.arDays(THIS.cboDays.VALUE)
* default the starting year at 2000 * set to starting year of choice. lnYrs = YEAR(DATE()) - 2000 +1 DECLARE THIS.arYears[m.lnYrs] FOR lni = 1 TO m.lnYrs * loads year, from 2000 to curent year, into an array * 2000, 2001, 2002, etc. THIS.arYears[lni] = ALLTRIM(STR(YEAR(DATE())- m.lnYrs + m.lni)) ENDFOR
DECLARE THIS.arMonths FOR lni = 1 to 12 * loads months (January, February, etc. into array) * I used 2001 as a base - could have used any year. lcCurrentMth = ALLTRIM(STR(m.lni)) + "/01/2001" THIS.arMonths[lni] = CMONTH(CTOD(m.lcCurrentMth)) * MESSAGEBOX(THIS.arMonths[lni]) ENDFOR
DECLARE THIS.arDays FOR lni = 1 to 31 THIS.arDays[lni] = PADL(ALLTRIM(STR(lni)),2,"0") ENDFOR