The Calendar Application
Page 2
The first problem I encountered was how to set the day names to the current language that the operating system is using. ( Win 95 allows you to change the language settings from the International Settings folder on the Control panel.)
Delphi relies upon a run-time library which contains an array with short day names. Presumably because these strings must be compiled into the component when it is installed into the Delphi IDE there is no way for a Delphi calendar component to dynamically adjust to the International Settings of the OS. The solution would be replace the call to ShortDayNames call with a array of day names read from the Operating System environment. Even the FormatDateTime function uses the strings from the ShortDayNames global variable.
VB3 also has the same limitations for some reason. Even though the syntax in VB3 is the same as that used in VB4 the expression Format(date,"Long Date") still always returns an English date when run on a Win95 machine using VB3.
The VB4 solution on the other hand was quite simple and straight forward. The International settings could be changed and these changes are immediately reflected in the way that VB4 formats date fields.
Create an array of seven labels and set their caption property to short date name of the day 1 through to 7. VB handles the necessary language conversions automatically.
Sub lblDay_Init()
' Initialize the lblDay() controls
Dim i As Integer
For i = 1 To 7
lblDay(i - 1).Caption = Format(WeekDay(i), "ddd")
Next i
End Sub
(To MS : Indonesian day names are not truncated to three characters when using
the "ddd" format string)
The next task was to provide a means to navigate through months and years to allow users to select both past and future dates. The Delphi Calendar component does not provide a means of doing this. When the Calendar component is implemented some other components must be added to the form container to navigate through the dates ( See the sample brDate.pas). The day, month and year attributes of the calendar component are implemented as properties of the component and so require additional controls to change the value of these properties during run-time.
For the VB equivalent I chose two dropdown combo box lists that are filled when the Calendar is initialized but you could use any other control (such as spin buttons or scrolls) to navigate through a value of months' and years.
Sub cboMonth_Init(C As ComboBox)
' Initialize the cboMonth control with a list of months in the users language
Dim txt As String
Dim i As Integer
C.Clear
For i = 1 To 12
C.AddItem Format$((33 * i) - 31, "MMMM")
Next
End Sub
Sub cboYear_Init(C As ComboBox)
' Fill a combo box with a list of 25 years from the current year - 10
Dim txt As String
Dim i As Integer
Dim s As Integer
Dim e As Integer
s = Val(Format$(Date, "YYYY")) - 10 ' Choose range of years to list
e = s + 50 ' Increase or decrease as required
i = s
C.Clear
Do Until i = e
C.AddItem Right$(Str$(i), 4)
i = i + 1
Loop
End Sub