WEBINAR:
On-Demand
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Programming the User Control
With the design of the user control completed, you can now start coding. Switch to the code behind file for the user control (
UserControl1.vb) and add the following namespace:
Imports System.ComponentModel
The System.ComponentModel namespace provides classes that are used to implement the run-time and design-time behavior of components and controls. In this case, you'll be adding attributes to the control class and its properties to customize its behavior. You will see the effects of these attributes shortly.
Next, declare the Formats and Months enumerations to represent the format and months, respectively:
Public Enum Formats
DDMMYYYY '---e.g. 29 02 2006---
DDMmmYYYY '---e.g. 12 Jan 2006---
MMDDYYYY '---e.g. 02 25 2006---
MmmDDYYYY '---e.g. Jan 15 2006---
End Enum
Public Enum Months
Jan = 1
Feb = 2
Mar = 3
Apr = 4
May = 5
Jun = 6
Jul = 7
Aug = 8
Sep = 9
Oct = 10
Nov = 11
Dec = 12
End Enum
Defining the Member Variables
Change the default class name from
UserControl1 to
DateControl and declare the member variables as shown below:
<DefaultEvent("Err")> _
Public Class DateControl
'---member variables---
Private _Format As Formats = Formats.DDMMYYYY
Private _Day As Integer
Private _Month As Integer
Private _year As Integer
Private _MinYear As Integer = 1918
Private _MaxYear As Integer = 2050
Private _DayControl As New ComboBox
Private _MonthControl As New ComboBox
'---event handler for error---
<Category("Date Settings"), _
DescriptionAttribute("Error event handler.")> _
Public Event Err(ByVal str As String)
End Class
You use the ControlDesigner class to extend the design mode behavior of user controls.
|
|
Notice the following:
- The control fires the Err event if a validation error occurs, providing the control with a way to communicate the error to the user.
- The Category attribute causes the Err event to appear under the Date Settings category in the VS Properties window. The DescriptionAttribute specifies the message VS displays at the bottom of the properties window.
- The DefaultEvent attribute indicates the default event handler for this control. When you drop this control on the design surface of a Windows Form, double-clicking it will automatically cause VS 2005 to insert stub code for the Err event handler.
- The _DayControl and the _MonthControl reference the respective ComboBox controls representing the day and month.
Next, add a
Format property to the class so users can select the date format. Depending on the format selected, the control uses the
_DayControl and
_MonthControl variables to reference the actual ComboBox controls representing the day and month. Also, when a user selects a format, the control calls the
InitDates() method (defined shortly) to refresh the control dynamically to reflect the selected format:
<Category("Date Settings"), _
DescriptionAttribute( _
"Sets the display format for " & _
"the control.")> _
Public Property Format() As Formats
Get
Return _Format
End Get
Set(ByVal value As Formats)
_Format = value
Select Case Me.Format
Case Formats.DDMmmYYYY, Formats.DDMMYYYY
_DayControl = cbbField1
_MonthControl = cbbField2
Case Formats.MMDDYYYY, Formats.MmmDDYYYY
_DayControl = cbbField2
_MonthControl = cbbField1
End Select
_DayControl.BackColor = Color.White
_MonthControl.BackColor = Color.White
InitDates()
End Set
End Property
The
Day property lets users set the day of the date individually, using a numeric value from 1 to 31. Notice that if the day value is set to an invalid value, the control raises the
Err event:
<Category("Date Settings"), _
DescriptionAttribute( _
"Sets the day value for the control.")> _
Public Property Day() As Integer
Get
Return _Day
End Get
Set(ByVal value As Integer)
If value >= 1 And value <= 31 Then
_Day = value
_DayControl.Text = _Day
Else
RaiseEvent Err("Day is out of range.")
End If
End Set
End Property
The
Month property lets users set the month to a numeric value from 1 to 12. While it is similar to the
Day property, you need to take special care when users select a format that displays the month alphabetically (such as Jan or Feb), because you must convert the numeric value of the month into its corresponding enumeration value:
<Category("Date Settings"), _
DescriptionAttribute( _
"Sets the month value for the control.")> _
Public Property Month() As Integer
Get
Return _Month
End Get
Set(ByVal value As Integer)
If value >= 1 And value <= 12 Then
_Month = value
If Me.Format = Formats.DDMMYYYY Or _
Me.Format = Formats.MMDDYYYY Then
_MonthControl.Text = _Month
Else
_MonthControl.Text = _
[Enum].GetName( _
GetType(Months), _Month)
End If
Else
RaiseEvent Err( _
"Month is out of range.")
End If
End Set
End Property
Creating the
Year property is much more straightforward:
<Category("Date Settings"), _
DescriptionAttribute( _
"Sets the year value for the control.")> _
Public Property Year() As Integer
Get
Return _year
End Get
Set(ByVal value As Integer)
_year = value
cbbYear.Text = _year
End Set
End Property
In addition to properties for setting the day, month, and year, the control has a
[Date] (the square brackets are because Date is a reserved word in VB) property to let users set the date using the Date class
:
<Category("Date Settings"), _
DescriptionAttribute( _
"Gets or sets the date for the control.")> _
Public Property [Date]() As Date
Get
Try
Dim d As New Date( _
Me.Year, Me.Month, Me.Day)
Return d
Catch ex As Exception
Return Nothing
End Try
End Get
Set(ByVal value As Date)
Me.Day = value.Day
Me.Month = value.Month
Me.Year = value.Year
End Set
End Property
The
MinYear and
MaxYear properties set the first and last years to display:
<Category("Date Settings"), _
DescriptionAttribute( _
"Sets the start year for the control.")> _
Public Property MinYear() As Integer
Get
Return _MinYear
End Get
Set(ByVal value As Integer)
_MinYear = value
End Set
End Property
<Category("Date Settings"), _
DescriptionAttribute( _
"Sets the end year for the control.")> _
Public Property MaxYear() As Integer
Get
Return _MaxYear
End Get
Set(ByVal value As Integer)
_MaxYear = value
End Set
End Property
As mentioned previously, the
InitDates() method refreshes the control so it reflects the currently selected format:
Private Sub InitDates()
'---day---
_DayControl.Items.Clear()
For i As Integer = 1 To 31
_DayControl.Items.Add(i)
Next
'---month---
_MonthControl.Items.Clear()
For i As Integer = 1 To 12
If Me.Format = _
Formats.DDMMYYYY Or Me.Format = _
Formats.MMDDYYYY Then
_MonthControl.Items.Add(i)
Else
_MonthControl.Items.Add( _
[Enum].GetName( _
GetType(Months), i))
End If
Next
'---year---
cbbYear.Items.Clear()
For i As Integer = Me.MinYear To Me.MaxYear
cbbYear.Items.Add(i)
Next
'---display the set date---
_DayControl.SelectedIndex = Me.Day - 1
_MonthControl.SelectedIndex = Me.Month - 1
cbbYear.Text = Me.Year
End Sub
The preceding method simply populates the ComboBox controls with the various dates so that the user can choose from them, and sets them to display the currently selected date.