Public Class EmployeePickerUserControl
Implements IRefreshableUserControl
Private ReadOnly _parentUserControl As IEmployeePickerParentUserControl
Private _employees As List(Of IEmployee)
Private Sub New()
' This call is required by the designer.
InitializeComponent()
End Sub
'''
''' Initialises the user form.
'''
''' The form containing this user control
Sub New(parentForm As IEmployeePickerParentUserControl)
'Call base constructor.
Me.New()
'Set parameters.
'Parent form.
'Value can't be Nothing.
If parentForm Is Nothing Then Throw New InternalException($"The parent user control can't be 'Nothing' in a {Me.Name}.")
_parentUserControl = parentForm
End Sub
'''
Public Function RefreshData() As Boolean Implements IRefreshableUserControl.RefreshData
Try
LoadData()
'Refresh succeeded.
Return True
Catch ex As Exception
'Refresh failed.
Return False
End Try
End Function
Private Sub EmployeePickerUserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
LoadData()
Catch ex As Exception
MessageBox.Show($"Could not load {Me.Name}.{vbNewLine}{ex.Message}", "An error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
'''
''' Load the employees into the combo box.
'''
Private Sub LoadData()
'Fetch the employees.
_employees = _parentUserControl.GetAllEmployees().OrderBy( Function(employee1) employee1.FirstName).ToList()
'Check if data is correct.
If _employees Is Nothing OrElse _employees.Count = 0 Then
'No items in collection
_employees = Nothing
End If
'Fill the combobox with names.
EmployeesComboBox.DataSource = _employees.Select(Function(employee) $"{employee.FirstName} {employee.LastName} ({employee.Initials})").ToArray()
End Sub
Private Sub EmployeesComboBox_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles EmployeesComboBox.SelectionChangeCommitted
'Skip if no employees were loaded.
If _employees Is Nothing Then Return
'Fetch the right employee.
Dim employee = _employees.Item(EmployeesComboBox.SelectedIndex)
'Send message to parent.
_parentUserControl.SelectEmployee(employee)
End Sub
End Class