Expanding on the Calendar Window Example

Hi,
I’m working with the calendar example to have Save and Cancel buttons instead of just closing itself when the user selects a date. I need the buttons because I’m also adding a time field to the window.

I would like the Calendar window to get a date from a database and show that date selected in the calendar when the window opens. To show that is the day selected, I would like to keep the bevel button for that day in the pushed position.

However, the code always sets the day of the date it’s given to day 1. It gives a reason for doing this but I still don’t understand. See below:

[code]Function GetDate(d As Date) As Double
//Call this from another window to allow user to select a date
//format: myDate.totalSeconds = Calendar.GetDate(CurrentDate)
//CurrentDate will be month displayed to start with, can be same as myDate

//Take the date given and set the day to one to prevent
//errors when a user advances a month with Jan31 for example
//then update window
mSelectedDate = New Date
mSelectedDate.TotalSeconds = d.Totalseconds

mSelectedDate.Day = 1
Update

//Stop code execution of this window until user chooses a date
Self.ShowModal

//User has picked a date, window is closing, return the new value
Return mSelectedDate.TotalSeconds
End Function
[/code]

Is it best to create another property to hold my initial date? I don’t want to mess anything up by using the calendar window property, mSelectedDate.

I planned on toggling the button in the Update() method like this:

[code] For calendarButton As Integer = 0 To 36
DayNum = calendarButton + 2 - dayOfWeek
If dayNum > 0 And dayNum <= monthDays Then
CalendarDateButton(calendarButton).Caption = Str(dayNum)
CalendarDateButton(calendarButton).Visible = True
Else
CalendarDateButton(calendarButton).Visible = False
End If

// Toggle the button of the selected day
bflag = false
// Set d to the currently selected date
d.month = MonthPopup.ListIndex + 1
d.year = Val( YearPopup.text )
d.day = DayNum
if d = mSelectedDate then bflag = true
CalendarDateButton(calendarButton).Value = bflag

Next[/code]

Thank you

Just to answer my own question and close this up. The calendar is displayed by working out which day of the week the month starts on. This is the reason why mSelectedDate Day cannot be any other value than 1.

So I replaced all occurrences of mSelectedDate with mBaseDate. Then I added another property called mSelectedDate date.

I changed the Action event of the calendar buttons to this:

[code]
// Don’t change the day because we’re not immediately closing the window and will possibly
// need to work off of the base date again if the user changes months.
//mBaseDate.Day = Val(Me.Caption)

mSelectedDate.month = MonthPopup.ListIndex + 1
mSelectedDate.year = Val( YearPopup.text )
mSelectedDate.day = Val(Me.Caption)

dim i as integer
for i = 0 to 36 // Make sure only selected day bevel button is toggled as pushed.
CalendarDateButton(i).Value = (i=index)
next[/code]

And the the snippet of code above in the Update() method to this:

// Toggle the button of the selected day bflag = false // Set d to the currently selected date d.month = MonthPopup.ListIndex + 1 d.year = Val( YearPopup.text ) d.day = DayNum if d.SQLDate = mSelectedDate.SQLDate then bflag = true CalendarDateButton(calendarButton).Value = bflag