Tutorials > Tutorial 8 - Using TDBCalendar with TDBDate |
In this tutorial, you will learn how to use the calendar control with the TDBDate control. The following sample overrides the built-in calendar with the calendar that will be set up on a second form.
Start a new project.
From the Visual Basic Project menu, select Components, then check the box labeled ComponentOne True DBCalendar 8.0. Click OK to add the TDBCalendar and control icon to the toolbox.
Place a True DBDate control (tdbDate) on the form (Form1) as shown in the following figure.
Add a new Form (Form2) to the project and place a True DBCalendar control (tdbCalendar) and command button (btnOk) on the form (Form2) as shown in the following figure.
Using the Button property page of tdbDate, set the DropDown (calendar) Position to 1 – Outside Control, and Visible to True. For the Spin button, setthe Position to 0 – Inside Control, Increment to 1, and Visible to True.
Using the Value property page of TDBDate, set the MinDate property to 100/1/1 and MaxDate to 2099/12/31.
This code will set the date to the current date.
Example Title |
Copy Code
|
---|---|
Private Sub Form_Load()
' Set date to today.
TDBDate1.Value = Now
End Sub
|
This code will set the dropdown calendar.
Example Title |
Copy Code
|
---|---|
Private Sub TDBDate1_DropOpen(NoDefault As Boolean) ' Ignore the internal calendar. NoDefault = True ' Need to have the calendar reflect the current date. Form2. TDBCalendar1.Value = TDBDate1.Value ' The date range also needs to be the same. Form2. TDBCalendar1.MinDate = TDBDate1.MinDate Form2. TDBCalendar1.MaxDate = TDBDate1.MaxDate ' Show the custom calendar control. Form2.Show vbModal End Sub |
This code will verify the date entered.
Example Title |
Copy Code
|
---|---|
Private Sub TDBDate1_InvalidValue(Restore As Boolean)
' By setting the Restore param to False, the previous value will not be restored, and the date will reset to Null.
Restore = False
End Sub
|
When the OK button is clicked on Form2, the selected date will be applied back to the Date control. The code is as follows:
Example Title |
Copy Code
|
---|---|
Private Sub btnOk_Click() ' Apply selected date back to control. Form1.TDBDate.Value = TDBCalendar1.Value ' Close form for [Esc] or [Cr] key. Unload Me End Sub |
When the ENTER or ESCAPE keys are pressed, this code will cause the focus to go back to Form1.
Example Title |
Copy Code
|
---|---|
Private Sub TDBCalendar1_KeyDown(KeyCode As Integer, Shift As Integer) ' Process the [Esc] and [Cr] keys. If KeyCode = vbKeyEscape Or KeyCode = vbKeyReturn Then ' Apply selected date back to control. If KeyCode = vbKeyReturn Then Form1. TDBDate1.Value = TDBCalendar1.Value End If ' Close form for [Esc] or [Cr] key. Unload Me End If End Sub |
The built-in calendar for the TDBDate control was replaced by the TDBCalendar control.
This concludes Tutorial 8.