Below are some code snippets from one of my programs. This is not a complete solution. It is an example of one (of many) way to use the
MonthCalendar
Windows Form control to allow user input and validation of dates. This example uses one
MonthCalendar
control and moves it to be next to the
TextBox
that is selected via the associated
Button
control.
To your Windows Form:
- Add MonthCalendar WinForms control to your Windows Form using Visual Studio Windows Form Designer named MonthCalendar.
- Add TextBox WinForms control for start date named txtEffectiveDate
- Add TextBox WinForms control for stop date named txtExpirationDate
- Add Button WinForms control to bring up calendar for Start date named btnEffectiveDate
- Add Button WinForms control to bring up calendar for Stop date named btnExpirationDate
- Add Button WinForms control to complete transaction named btnOK
' Declare some global variables
Dim objTextbox as Textbox
Dim bError_EffectiveDate as Boolean
Dim bError_ExpirationDate as Boolean
' Event for Date Selected
Private Sub MonthCalendar_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar.DateSelected
bError_StartTime = False
bError_StopTime = False
objDateTextBox.Text = Format$(MonthCalendar.SelectionStart, "MM/dd/yyyy")
Select Case objDateTextBox.Name
Case "txtEffectiveDate"
Case "txtExpirationDate"
Case Else
End Select
Call ToggleCalendarButtons(True)
btnOK.Enabled = True
txtStartTime.Focus()
End Sub
' Toggle show or hide the MonthCalendar control
Private Sub ToggleCalendarButtons(ByVal TRUEFALSE As Boolean)
MonthCalendar.Visible = Not TRUEFALSE
If Not TRUEFALSE Then
MonthCalendar.BringToFront()
End If
btnEffectiveDate.Enabled = TRUEFALSE
btnExpirationDate.Enabled = TRUEFALSE
btnOK.Enabled = TRUEFALSE
Try
MonthCalendar.SetDate(CDate(objDateTextBox.Text))
Catch
End Try
objDateTextBox.Focus()
End Sub
' Handle click event for Effective Date and Expiration Date buttons
Private Sub btnEffectiveDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEffectiveDate.Click
btnEffectiveDate.Enabled = False
If Not MonthCalendar.Visible Then
MonthCalendar.Location = New Point(btnEffectiveDate.Location.X, btnEffectiveDate.Location.Y)
objDateTextBox = txtEffectiveDate
Call ToggleCalendarButtons(False)
End If
btnEffectiveDate.Enabled = True
End Sub
Private Sub btnExpirationDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExpirationDate.Click
btnExpirationDate.Enabled = False
If Not MonthCalendar.Visible Then
MonthCalendar.Location = New Point(btnExpirationDate.Location.X, btnExpirationDate.Location.Y)
objDateTextBox = txtExpirationDate
Call ToggleCalendarButtons(False)
End If
btnExpirationDate.Enabled = True
End Sub
' Validate date in case use keyed it instead of selecting from the calendar
Private Sub txtExpirationDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtExpirationDate.Validating
If txtExpirationDate.Text.Length > 0 Then
Try
txtExpirationDate.Text = Format$(CDate(txtExpirationDate.Text.ToString), "MM/dd/yyyy")
bError_ExpirationDate = False
Catch myException As InvalidCastException
MsgBox(myException.Message, MsgBoxStyle.Exclamation, "Expires Date")
bError_ExpirationDate = True
End Try
End If
End Sub
Private Sub txtEffectiveDate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtEffectiveDate.Validating
If txtEffectiveDate.Text.Length > 0 Then
Try
txtEffectiveDate.Text = Format$(CDate(txtEffectiveDate.Text.ToString), "MM/dd/yyyy")
bError_EffectiveDate = False
Catch myException As InvalidCastException
MsgBox(myException.Message, MsgBoxStyle.Exclamation, "Effective Date")
bError_EffectiveDate = True
End Try
End If
End Sub