Using+the+Date+and+Time+Picker

//by Richard Russell, May 2006//

One of the standard controls provided by the Windows API is the Date and Time Picker. This can be used when you want the user to enter a date or a time into your program. For date entry Windows can display a small clickable calendar:



You can display the Date/Time Picker either on your main output window or in a dialogue box.

Output window
To display a Date/Time picker on the output window use code similar to the following:

code format="bb4w" INSTALL @lib$+"WINLIB5" ICC_DATE_CLASSES = &100

DIM iccx{Size%, ICC%} : REM INITCOMMONCONTROLSEX structure iccx.Size% = 8 iccx.ICC% = ICC_DATE_CLASSES SYS "InitCommonControlsEx", iccx{}

hdtp% = FN_createwindow("SysDateTimePick32", "", x%, y%, cx%, cy%, 0, style%, 0) code Here **x%,y%** is the position at which the Date/Time picker should be displayed, **cx%,cy%** is the size of the Date/Time picker window and **style%** is one of the following values (the figures in brackets are suggested window sizes for the different styles):


 * **0** - to display a date picker using the short date format (cx%=88, cy%=20)
 * **4** - to display a date picker using the long date format (cx%=120, cy%=20)
 * **9** - to display a time picker (cx%=72, cy%=20)

To read the currently selected date or time from the control use code similar to the following:

code format="bb4w" DIM systemtime{Year{l&,h&}, Month{l&,h&}, DayOfWeek{l&,h&}, Day{l&,h&}, \ \             Hour{l&,h&}, Minute{l&,h&}, Second{l&,h&}, Milliseconds{l&,h&}} DTM_GETSYSTEMTIME = &1001 SYS "SendMessage", hdtp%, DTM_GETSYSTEMTIME, 0, systemtime{}

day% = systemtime.Day.l&     month% = systemtime.Month.l&      year% = systemtime.Year.h& * 256 + systemtime.Year.l&      hour% = systemtime.Hour.l&      minute% = systemtime.Minute.l&      second% = systemtime.Second.l& code To close the control use the following code:

code format="bb4w" PROC_closewindow(hdtp%) code

Dialogue box
To display a Date/Time picker in a dialogue box use code similar to the following:

code format="bb4w" INSTALL @lib$+"WINLIB2" ICC_DATE_CLASSES = &100 WS_VISIBLE = &10000000 WS_CHILD = &40000000 WS_GROUP = &20000 BS_DEFPUSHBUTTON = 1 DIM iccx{Size%, ICC%} : REM INITCOMMONCONTROLSEX structure iccx.Size% = 8 iccx.ICC% = ICC_DATE_CLASSES SYS "InitCommonControlsEx", iccx{} dtpdlg% = FN_newdialog("Date/Time picker", 20, 20, 150, 140, 8, 300) PROC_dlgctrl(dtpdlg%, "", 101, 8, 8, 64, 14, \      \            WS_VISIBLE + WS_CHILD + style%, "SysDateTimePick32") PROC_pushbutton(dtpdlg%, "OK", 1, 12, 120, 56, 14, WS_GROUP + BS_DEFPUSHBUTTON) PROC_pushbutton(dtpdlg%, "Cancel", 2, 80, 120, 56, 14, 0) PROC_showdialog(dtpdlg%) code Where **style%** has one of the values listed above. Again the size of the Date/Time picker control will need to be modified according to the style, but here the size is specified in dialogue box units rather than pixels (see the main [|BBC BASIC for Windows documentation]).

To read the currently selected date or time from the control when the OK button is clicked use code similar to the following:

code format="bb4w" click% = 0 ON SYS click% = @wparam% AND &FFFF : RETURN REPEAT WAIT 1 UNTIL !dtpdlg% = 0 OR click% = 1 OR click% = 2

ON SYS OFF DTM_GETSYSTEMTIME = &1001 IF click% = 1 THEN DIM systemtime{Year{l&,h&}, Month{l&,h&}, DayOfWeek{l&,h&}, Day{l&,h&}, \ \             Hour{l&,h&}, Minute{l&,h&}, Second{l&,h&}, Milliseconds{l&,h&}} SYS "SendDlgItemMessage", !dtpdlg%, 101, DTM_GETSYSTEMTIME, 0, systemtime{}

day% = systemtime.Day.l&       month% = systemtime.Month.l&        year% = systemtime.Year.h& * 256 + systemtime.Year.l&        hour% = systemtime.Hour.l&        minute% = systemtime.Minute.l&        second% = systemtime.Second.l&      ENDIF

PROC_closedialog(dtpdlg%) code You will want to use **ON ERROR** and **ON CLOSE** statements to ensure that is called even if the program is terminated unexpectedly.