Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Month, Day, Year Picker

Forms & Screen

Month, Day, Year Picker

by  jimoo  Posted    (Edited  )
[img http://www.deltabg.com/images/datepicker.gif]


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.


**************************************************
*-- Class: monthdayyear
*-- ParentClass: container
*-- BaseClass: container
*

* Written by Jim Osieczonek - Delta Business Group, LLC
* www.deltabg.com

SET CENTURY ON

LOCAL loObject
loObject = CREATEOBJECT('monthdayyear')

PUBLIC poMyForm
poMyForm=NEWOBJECT("form1")
poMyForm.addobject('conDate','monthdayyear')
poMyForm.conDate.visible =.t.
poMyForm.Show
RETURN

DEFINE CLASS form1 AS form
DoCreate = .T.
Name = "Form1"
Caption = "Super-Duper Date Picker"
ENDDEFINE

DEFINE CLASS monthdayyear AS container

Width = 224
Height = 46
BorderWidth = 0
actualdate = {}
Name = "monthdayyear"


*-- Holds the proper number of days for the combo box [January = 31, February = 28/29, March = 31, etc.]
DIMENSION ardays[1]

*-- Array to hold the Character Name of the Months [January, February, etc.]
DIMENSION armonths[1]

*-- Array to hold the years [2000, 2001, etc.]
DIMENSION aryears[1]


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


ENDPROC


PROCEDURE makeactualdate
LOCAL lcDay,lcMonth,lcYear
lcDay = THIS.arDays(THIS.cboDays.VALUE)

lcMonth = PADL(THIS.cboMonths.VALUE,2,"0")
lcYear = THIS.arYears(THIS.cboYears.VALUE)


THIS.actualDate = CTOD(m.lcMonth + "/" + m.lcDay + "/" + m.lcYear)
*MESSAGEBOX(this.actualdate)
MESSAGEBOX("You Selected: " + DTOC(THIS.actualDate),0,"Drum Roll...")
ENDPROC

PROCEDURE Init
LOCAL lnYrs,lcCurrentMth,lni

* 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[12]
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[31]
FOR lni = 1 to 31
THIS.arDays[lni] = PADL(ALLTRIM(STR(lni)),2,"0")
ENDFOR

THIS.cboMonths.VALUE = 1
THIS.cboYears.VALUE = 1
THIS.cboDays.VALUE = 1

THIS.cboDays.REQUERY
THIS.cboMonths.REQUERY
THIS.cboYears.REQUERY

ENDPROC

PROCEDURE cbomonths.Click
THIS.PARENT.checkTheDays
THIS.PARENT.makeActualDate
ENDPROC


PROCEDURE cbodays.Click
THIS.PARENT.makeActualDate
ENDPROC


PROCEDURE cboyears.Click
THIS.PARENT.checkTheDays
THIS.PARENT.makeActualDate
ENDPROC

ENDDEFINE
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top