Imports ProductiePitching.configs Public Class EmployeeUserControl Implements IEmployeeUserControl, IEmployeeDetailParentUserControl, IEmployeePickerParentUserControl Private ReadOnly _parentForm As IEmployeesMainForm Private _currentUserControl As IRefreshableUserControl Private ReadOnly Property EmployeeIndex As Integer Private Sub New() ' This call is required by the designer. InitializeComponent() End Sub ''' ''' Initialises the user form. ''' ''' The form containing this user control ''' The index of the employee, 0 based Sub New(parentForm As IEmployeesMainForm, employeeIndex As Integer) 'Call base constructor. Me.New() 'Set parameters. 'Parent form. 'Value can't be Nothing. If parentForm Is Nothing Then Throw New InternalException($"The parent form can't be 'Nothing' in a {Me.Name}.") _parentForm = parentForm 'Grid number. Me.EmployeeIndex = employeeIndex End Sub Private Sub EmployeeUserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try 'Set back color corresponding to the employee index. BackColor = Color.White ForeColor = Color.Black 'Set border style. BorderStyle = BorderStyle.FixedSingle 'Show the Employee picker user control. SwitchUserControl(New EmployeePickerUserControl(Me)) 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 ''' ''' Update border of the user control. ''' Private Sub EmployeeUserControl_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint 'Border color. Dim borderColor = ConfigsLoader.ColorsEmployees(EmployeeIndex) 'Border thickness. Const borderThickness As Integer = 5I 'Border rectangle. Dim borderRectangle = Me.ClientRectangle 'Draw the border. e.Graphics.DrawRectangle(New Pen(borderColor, borderThickness), borderRectangle) End Sub ''' ''' Switch to another user control on the screen. ''' ''' Must be a refreshable user control ''' Private Sub SwitchUserControl(Of T As {IRefreshableUserControl, UserControl})(userControl As T) 'Set current user control. _currentUserControl = userControl 'Remove the current user control from the screen. UserControlPanel.Controls.Clear() 'Make it exactly as big as the panel it will go into. userControl.Size = UserControlPanel.Size 'Add it onto the panel. UserControlPanel.Controls.Add(userControl) End Sub ''' Public Function RefreshData() As Boolean Implements IRefreshableUserControl.RefreshData 'Update the current user control. Return _currentUserControl.RefreshData() End Function #Region "Employee picking" ''' Public Function GetAllEmployees() As ICollection(Of IEmployee) Implements IEmployeePickerParentUserControl.GetAllEmployees Return _parentForm.GetAllEmployees() End Function ''' Public Sub SelectEmployee(employee As IEmployee) Implements IEmployeePickerParentUserControl.SelectEmployee Try 'Call parent to select employee. _parentForm.SelectEmployee(EmployeeIndex, employee) 'Load detail user control. SwitchUserControl(New EmployeeDetailUserControl(Me)) Catch ex As Exception MessageBox.Show($"Could not select employee.{vbNewLine}{ex.Message}", "An error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub ''' Public Sub SelectedEmployeeChanged() Implements IEmployeeUserControl.SelectedEmployeeChanged Try 'If the current user control is to select the employee, make it refresh. If TypeOf _currentUserControl Is EmployeePickerUserControl Then _currentUserControl.RefreshData() End If Catch ex As Exception MessageBox.Show($"Could not update after employee selection changed.{vbNewLine}{ex.Message}", "An error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub #End Region #Region "Employee details" ''' Public Function FetchEmployee() As IEmployee Implements IEmployeeDetailParentUserControl.FetchEmployee Return _parentForm.GetSelectedEmployee(EmployeeIndex) End Function ''' Public Sub DeselectEmployee() Implements IEmployeeDetailParentUserControl.DeselectEmployee Try 'Call parent to deselect employee. _parentForm.DeselectEmployee(EmployeeIndex) 'Switch to employee picker user control. SwitchUserControl(New EmployeePickerUserControl(Me)) Catch ex As Exception MessageBox.Show($"Could not deselect employee.{vbNewLine}{ex.Message}", "An error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub ''' Public Sub SelectedActivityChanged() Implements IEmployeeUserControl.SelectedActivityChanged Try 'If the current user control is to select the employee, make it refresh. If TypeOf _currentUserControl Is EmployeeDetailUserControl Then _currentUserControl.RefreshData() End If Catch ex As Exception MessageBox.Show($"Could not update after activity selection changed.{vbNewLine}{ex.Message}", "An error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub ''' Public Function GetCurrentlySelectedActivity() As IActivity Implements IEmployeeDetailParentUserControl.GetCurrentlySelectedActivity Return _parentForm.GetCurrentlySelectedActivity() End Function ''' Public Sub StartActivity() Implements IEmployeeDetailParentUserControl.StartActivity 'Start activity with correct employee. _parentForm.StartActivity(EmployeeIndex) End Sub ''' Public Sub FinishActivity() Implements IEmployeeDetailParentUserControl.FinishActivity _parentForm.FinishActivity(EmployeeIndex) End Sub ''' Public Sub StopActivity() Implements IEmployeeDetailParentUserControl.StopActivity _parentForm.StopActivity(EmployeeIndex) End Sub #End Region End Class