Imports ProductiePitching.configs Public Class ProductiePitchingForm Implements IWorkItemsMainForm, IEmployeesMainForm Private _datagridViewUserControls As List(Of IDataGridUserControl) Private _employeeUserControls As List(Of IEmployeeUserControl) Private _domainController As IDomainController Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try 'Variable initialisation, putting this in a constructor breaks 'DatagridViewControlsFlowLayoutPanel.Controls.Add' -> null reference exception. _datagridViewUserControls = New List(Of IDataGridUserControl) _employeeUserControls = New List(Of IEmployeeUserControl) _domainController = DomainController.GetInstance() 'Set the color of the form. BackColor = Color.White ForeColor = Color.Black 'Update size and set center position. Size = New System.Drawing.Size(ConfigsLoader.AppWidth, ConfigsLoader.AppHeight) StartPosition = FormStartPosition.CenterScreen 'Set the title name of the form from the config. Text = $"Productie pitching - {ConfigsLoader.AppName}" GridLabel.Text = ConfigsLoader.AppName 'Load the user controls with the data grids. LoadDataGridUserControls() 'Load the user controls with the employees. LoadEmployeeUserControls() PauseActivitiesFromApp() 'Set the timer's value in miliseconds. RefreshTimer.Interval = ConfigsLoader.RefreshIntervalSeconds * 1000 'Start the times. RefreshTimer.Start() Catch ex As Exception MessageBox.Show(ex.Message, "An error occured...", MessageBoxButtons.OK, MessageBoxIcon.Error) Application.Exit() End Try End Sub Private Sub PauseActivitiesFromApp() _domainController.PauseActivitiesFromApp() End Sub 'Refreshes all data. If fails, updates label with last timestamp of refresh. Private Sub RefreshAllData() Dim allSuccessfulRefresh = RefreshUserGridsData() allSuccessfulRefresh = allSuccessfulRefresh And RefreshEmployeesData() 'Check if refresh was successful. If allSuccessfulRefresh Then 'Update latest refresh time label with current time. RefreshTimeLabel.Text = TimeOfDay.ToString("HH:mm") RefreshFailedLabel.Visible = False Else RefreshFailedLabel.Visible = True End If End Sub 'Refreshes at specified interval. Private Sub RefreshTimer_Tick(sender As Object, e As EventArgs) Handles RefreshTimer.Tick 'Refresh all the data. RefreshAllData() End Sub #Region "Data grids" ''' ''' Loads the user controls with the data grid. ''' Private Sub LoadDataGridUserControls() Try 'Loop for every data grid view, number of grids gotten from config. For gridNumber = 0 To ConfigsLoader.NumberOfGrids - 1 'Initialise the user control and set parent form and grid number. Dim userControl As New DataGridViewUserControl(Me, gridNumber) 'Set other parameters. With userControl .Size = New System.Drawing.Size(ConfigsLoader.GridWidths(gridNumber), ConfigsLoader.GridHeights(gridNumber)) End With 'Add to list of user controls. _datagridViewUserControls.Add(userControl) 'Add to layout panel. DatagridViewControlsFlowLayoutPanel.Controls.Add(userControl) Next Catch ex As Exception Throw New Exception($"Could not load data grid user controls.{vbNewLine}{ex.Message}") End Try End Sub ''' Public Function GetWorkItems(gridNumber As Integer) As List(Of IWorkItem) Implements IWorkItemsMainForm.GetWorkItems Return _domainController.GiveWorkItems(gridNumber) End Function Private _selectedActivity As IActivity ''' Public Sub ChangeSelectedActivity(activity As IActivity, gridNumber As Integer) Implements IWorkItemsMainForm.ChangeSelectedActivity 'Store the activity. _selectedActivity = activity 'If the activity is Nothing, no activity was selected, meaning that everything should be cleared. If activity Is Nothing Then 'Clear selection for every grid user control. _datagridViewUserControls.ForEach(Sub(userControl) userControl.RemoveSelection()) Else 'Clear selection of every grid except the one with the grid number. For Each control In _datagridViewUserControls 'Remove selection from every other grid. If Not control.GridNumber = gridNumber Then control.RemoveSelection() Next End If 'Update employee user controls. _employeeUserControls.ForEach(Sub(employeeUserControl) employeeUserControl.SelectedActivityChanged()) End Sub ''' Public Function GetCurrentlySelectedActivity() As IActivity Implements IActivityMainForm.GetCurrentlySelectedActivity Return _selectedActivity End Function ''' ''' Try to refresh all user grids and register time of latest refresh. ''' Private Function RefreshUserGridsData() As Boolean Try 'Reset selected activity because none will be selected after refresh. _selectedActivity = Nothing 'Refresh every user control. Register whether or not all refreshes were successful. Return _datagridViewUserControls.All(Function(userControl) userControl.RefreshData()) Catch ex As Exception Return False End Try End Function #End Region #Region "Employees" ''' ''' Loads the user controls with the employee data. ''' Private Sub LoadEmployeeUserControls() Try 'Loop for every employee, number of grids gotten from config. For employeeIndex = 0 To ConfigsLoader.NumberOfEmployees - 1 'Initialise the user control and set parent form and grid number. Dim userControl As New EmployeeUserControl(Me, employeeIndex) 'Set other parameters. With userControl .Size = New System.Drawing.Size(250, 300) 'todo make configurable End With 'Add to list of user controls. _employeeUserControls.Add(userControl) 'Add to layout panel. EmployeesControlsFlowLayoutPanel.Controls.Add(userControl) Next Catch ex As Exception Throw New Exception($"Could not load the employee user controls.{vbNewLine}{ex.Message}") End Try End Sub ''' ''' Refresh the employee controls with employee data. ''' ''' Private Function RefreshEmployeesData() As Boolean Try 'Refresh every user control. Register whether or not all refreshes were successful. Return _employeeUserControls.All(Function(userControl) userControl.RefreshData()) Catch ex As Exception Return False End Try End Function ''' Public Function GetAllEmployees() As ICollection(Of IEmployee) Implements IEmployeesMainForm.GetAllEmployees Return _domainController.GetAvailableEmployees() End Function ''' Public Sub SelectEmployee(employeeIndex As Integer, employee As IEmployee) Implements IEmployeesMainForm.SelectEmployee 'Call function in dc. _domainController.SelectEmployee(employeeIndex, employee) 'Send message that the selected employee changed to all user controls. _employeeUserControls.ForEach(Sub(employeeUserControl) employeeUserControl.SelectedEmployeeChanged()) 'Refresh all the grids, so data grid can recolor. This removes the selection, so the employee controls also need to be updated. RefreshAllData() End Sub ''' Public Sub DeselectEmployee(employeeIndex As Integer) Implements IEmployeesMainForm.DeselectEmployee _domainController.DeselectEmployee(employeeIndex) 'Send message that the selected employee changed to all user controls. _employeeUserControls.ForEach(Sub(employeeUserControl) employeeUserControl.SelectedEmployeeChanged()) 'Refresh the grids, so one can recolor. This removes the selection, so the employee controls also need to be updated. RefreshAllData() End Sub ''' Public Function GetSelectedEmployee(employeeIndex As Integer) As IEmployee Implements IEmployeesMainForm.GetSelectedEmployee Return _domainController.GetSelectedEmployee(employeeIndex) End Function Public Sub StartActivity(employeeIndex As Integer) Implements IEmployeesMainForm.StartActivity _domainController.StartActivity(_selectedActivity, employeeIndex) 'Refresh all the data. RefreshAllData() End Sub Public Sub FinishActivity(employeeIndex As Integer) Implements IEmployeesMainForm.FinishActivity _domainController.FinishActivity(employeeIndex) 'Refresh all the data. RefreshAllData() End Sub Public Sub StopActivity(employeeIndex As Integer) Implements IEmployeesMainForm.StopActivity _domainController.StopActivity(employeeIndex) 'Refresh all the data. RefreshAllData() End Sub Private Sub ProductiePitchingForm_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown 'Load all the data. Not done with the general method, because we want to catch the exceptions. RefreshAllData() End Sub #End Region End Class