Lots of routines, or so I think.

jwcoleman87

Well-known member
Joined
Oct 4, 2014
Messages
124
Programming Experience
Beginner
Do complicated forms generally have this many routines?

Is my class abnormally large?

    'STARTUP ROUTINE
    Private Sub Repair_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoginInfo.ShowDialog()
        If SQL.Login(LoginInfo.User, LoginInfo.Pwd) = True Then Me.Enabled = True
        RefreshForm()
    End Sub

    'SUBROUTINES FOR HANDLING BUTTON CLICKS
    Private Sub cmdNewRepair_Click() Handles cmdNewRepair.Click
        Using db As New ProductionDataModelDataContext
            Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = txtSelectedAssigned.Text).First
            SelectedRepair.StartTime = DateTime.Now()
            SelectedRepair.Status = "Unit Repair"
            db.SubmitChanges()
            ClearSelections()
            RefreshForm()
        End Using
    End Sub
    Private Sub cmdCompleteRepair_Click(sender As Object, e As EventArgs) Handles cmdCompleteRepair.Click
        Using db As New ProductionDataModelDataContext
            If txtSelectedRepairing.Text <> "" Then
                Dim CSRFlag As Integer
                'CSR Label Logic
                If cbCSRLabel.SelectedItem = "Yes" Then
                    CSRFlag = 1
                ElseIf cbCSRLabel.SelectedItem = "No" Then
                    CSRFlag = 0
                Else
                    MsgBox("You must select Yes or No to send a label to CSR")
                    Exit Sub
                End If
                Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = txtSelectedRepairing.Text).First
                SelectedRepair.EndTime = DateTime.Now()
                SelectedRepair.Status = Status
                SelectedRepair.CSRQuoteFlag = CSRFlag

                db.SubmitChanges()
                ClearSelections()
                RefreshForm()
            End If
        End Using
    End Sub
    Private Sub btnAcceptAssignment_Click() Handles btnAcceptAssignment.Click '
        Using db As New ProductionDataModelDataContext
            Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = txtSerialNumber.Text.Trim()).FirstOrDefault
            If IsNothing(SelectedRepair) Then
                Dim newAssignment As New Repair With _
                    {.SerialNumber = txtSerialNumber.Text, _
                     .TechnicianID = LoginInfo.User, _
                     .Status = "Assigned"}
                db.Repairs.InsertOnSubmit(newAssignment)
            Else
                SelectedRepair.TechnicianID = LoginInfo.User
                SelectedRepair.Status = "Assigned"
            End If
            db.SubmitChanges()
            RefreshForm()
            ClearSelections()
        End Using
    End Sub

    'SUBROUTINES FOR DRAGGING AND DROPPING ITEMS FROM ACTIVE REPAIRS TO ASSIGNED REPAIRS
    Private Sub listActiveRepairs_ItemDrag(ByVal sender As Object, ByVal e As MouseEventArgs) Handles listActiveRepairs.MouseDown
        If listActiveRepairs.SelectedItem = Nothing Then
            Exit Sub
        Else
            listActiveRepairs_SelectedIndexChanged(Me, e)
            listActiveRepairs.DoDragDrop(listActiveRepairs.SelectedItem.ToString(), DragDropEffects.Copy Or DragDropEffects.Move)
        End If
    End Sub
    Private Sub listAssignedRepairs_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles listStaging.DragEnter
        e.Effect = DragDropEffects.Copy
    End Sub
    Private Sub listAssignedRepairs_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles listStaging.DragDrop
        txtSerialNumber.Text = e.Data.GetData(DataFormats.Text)
        btnAcceptAssignment_Click()
    End Sub

    'SUBROUTINES FOR FORM DATA UPDATES, MAINTENANCE, AND BEHAVIORS
    Private Sub listActiveRepairs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listActiveRepairs.SelectedIndexChanged
        Using db As New ProductionDataModelDataContext
            If listActiveRepairs.SelectedItem() <> "" Then
                Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = listActiveRepairs.SelectedItem.ToString).First
                txtStartTime.Text = SelectedRepair.StartTime
                txtEndTime.Text = Nothing
                txtTimeAllocated.Text = DateTime.Now().Subtract(SelectedRepair.StartTime).ToString()
                txtStatus.Text = SelectedRepair.Status
                txtSelectedRepairing.Text = SelectedRepair.SerialNumber
            End If
        End Using
    End Sub
    Private Function listInactiveRepairs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listInactiveRepairs.SelectedIndexChanged
        Using db As New ProductionDataModelDataContext
            If listInactiveRepairs.SelectedItem <> Nothing Then
                Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = listInactiveRepairs.SelectedItem().ToString()).First
                txtStartTime.Text = SelectedRepair.StartTime.ToString()
                txtEndTime.Text = SelectedRepair.EndTime.ToString()
                txtStatus.Text = SelectedRepair.Status
            End If
            Return 0
        End Using

    End Function
    Private Sub listStaging_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listStaging.SelectedIndexChanged
        If listStaging.SelectedItem <> Nothing Then
            txtSelectedAssigned.Text = listStaging.SelectedItem.ToString()
        End If

    End Sub
    Private Sub txtSerialNumber_KeyPress(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtSerialNumber.KeyDown
        If (e.KeyValue = Keys.Enter) Then
            btnAcceptAssignment_Click()
        End If
    End Sub
    Private Function UpdateTechDetails() Handles dtpTechDetails.ValueChanged
        Try
            SQL.ListInactiveRepairs(listInactiveRepairs, LoginInfo.User, dtpTechDetails.Value().Date())
            txtCompletedRepairs.Text = TotalRepairs(LoginInfo.User, dtpTechDetails.Value().Date(), "Final Test")
            txtAPRepairs.Text = TotalRepairs(LoginInfo.User, dtpTechDetails.Value().Date(), "Awaiting Parts")
            txtPQRepairs.Text = TotalRepairs(LoginInfo.User, dtpTechDetails.Value().Date(), "Pre-Quote")
            txtCSRRepairs.Text = TotalRepairs(LoginInfo.User, dtpTechDetails.Value().Date(), "Customer Supporting")
            Return 1
        Catch ex As Exception
            MsgBox(ex.Message())
            Return 0
        End Try
    End Function
    Private Sub ClearSelections()
        txtSelectedAssigned.Clear()
        txtSelectedRepairing.Clear()
        txtSerialNumber.Clear()
        rbAP.Checked = False
        rbPQ.Checked = False
        rbFT.Checked = False
        rbBOM.Checked = False
        Status = ""
        cbCSRLabel.Text = ""
    End Sub
    Public Sub RefreshForm() ' TODO - Add capability to copy items from the lists to clipboard
        Using db As New ProductionDataModelDataContext
            Dim AssignedRepairsQuery = From t In db.Repairs _
                                       Where t.TechnicianID = LoginInfo.User _
                                       And t.Status = "Assigned" _
                                       Select t.SerialNumber

            Dim ActiveRepairsQuery = From t In db.Repairs _
                                     Where t.TechnicianID = LoginInfo.User _
                                     And t.Status = "Unit Repair" _
                                     Select t.SerialNumber
            Dim InactiveRepairsQuery = From t In db.Repairs _
                                       Where t.TechnicianID = LoginInfo.User _
                                       And t.Status <> "Unit Repair" _
                                       And t.Status <> "Pause" _
                                       And t.Status <> "Assigned" _
                                       And t.EndTime.Value.Date = dtpTechDetails.Value.Date()
                                       Select t.SerialNumber

            listActiveRepairs.DataSource = ActiveRepairsQuery
            listStaging.DataSource = AssignedRepairsQuery
            listInactiveRepairs.DataSource = InactiveRepairsQuery
        End Using
    End Sub

    'SUBROUTINES FOR RADIO BUTTONS USED WHEN SENDING UNITS TO AN INACTIVE STATUS
    Private Sub rbAP_CheckedChanged(sender As Object, e As EventArgs) Handles rbAP.CheckedChanged
        Status = rbAP.Text
    End Sub
    Private Sub rbPQ_CheckedChanged(sender As Object, e As EventArgs) Handles rbPQ.CheckedChanged
        Status = rbPQ.Text
    End Sub
    Private Sub rbCSR_CheckedChanged(sender As Object, e As EventArgs) Handles rbCSR.CheckedChanged
        Status = rbCSR.Text
    End Sub
    Private Sub rbFT_CheckedChanged(sender As Object, e As EventArgs) Handles rbFT.CheckedChanged
        Status = rbFT.Text
    End Sub
    Private Sub rbBOM_CheckedChanged(sender As Object, e As EventArgs) Handles rbBOM.CheckedChanged
        Status = rbBOM.Text
    End Sub

    'SUBROUTINES FOR ASSIGNED UNITS CONTEXT MENU
    Private Sub RepairInteraction_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles AssignedUnitInteractions.Opening
        If listStaging.SelectedItems.Count = 0 Then
            RemoveToolStripMenuItem.Enabled = False
            CopyToolStripMenuItem.Enabled = False
            ChangeSerialToolStripMenuItem.Enabled = False
        Else
            RemoveToolStripMenuItem.Enabled = True
            CopyToolStripMenuItem.Enabled = True
            ChangeSerialToolStripMenuItem.Enabled = True
        End If

    End Sub
    Private Sub RemoveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem.Click
        Using db As New ProductionDataModelDataContext
            Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = listStaging.SelectedItem.ToString()).First
            SelectedRepair.Status = "NULL"
            SelectedRepair.TechnicianID = "NULL"
            db.SubmitChanges()
            RefreshForm()
            ClearSelections()
        End Using
    End Sub
    Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
        Clipboard.SetData(DataFormats.Text, listStaging.Text)
    End Sub
    Private Sub tsTxtNewSerial_Click(sender As Object, e As EventArgs) Handles tsTxtNewSerial.Click
        tsTxtNewSerial.Text = ""
    End Sub
    Private Sub ConfirmToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ConfirmToolStripMenuItem.Click
        Using db As New ProductionDataModelDataContext
            Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = listStaging.SelectedItem.ToString()).First
            db.Repairs.DeleteOnSubmit(SelectedRepair)
            db.SubmitChanges()
            RefreshForm()
            ClearSelections()
        End Using
    End Sub

    'SUBROUTINES FOR INACTIVE UNITS CONTEXT MENU
    Private Sub InactiveRepairsInteraction_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles InactiveRepairsInteraction.Opening
        If listInactiveRepairs.SelectedItems.Count = 0 Then
            CopyToolStripMenuItem.Enabled = False
            ChangeDispositionToolStripMenuItem.Enabled = False
        Else
            CopyToolStripMenuItem.Enabled = True
            ChangeDispositionToolStripMenuItem.Enabled = True
        End If
    End Sub
    Private Sub CopyToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem1.Click
        Clipboard.SetData(DataFormats.Text, listStaging.Text)
    End Sub
    Private Sub AwaitingPartsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AwaitingPartsToolStripMenuItem.Click
        ChangeDisposition(listInactiveRepairs.SelectedItem, AwaitingPartsToolStripMenuItem.ToString())
        RefreshForm()
        UpdateTechDetails()
        ClearSelections()
    End Sub
    Private Sub CustomerSupportingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CustomerSupportingToolStripMenuItem.Click
        ChangeDisposition(listInactiveRepairs.SelectedItem.Text, CustomerSupportingToolStripMenuItem.ToString())
        RefreshForm()
        UpdateTechDetails()
        ClearSelections()
    End Sub
    Private Sub FinalTestToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FinalTestToolStripMenuItem.Click
        ChangeDisposition(listInactiveRepairs.SelectedItem, FinalTestToolStripMenuItem.ToString())
        RefreshForm()
        UpdateTechDetails()
        ClearSelections()
    End Sub
    Private Sub EngineeringHoldToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EngineeringHoldToolStripMenuItem.Click
        ChangeDisposition(listInactiveRepairs.SelectedItem, EngineeringHoldToolStripMenuItem.ToString())
        RefreshForm()
        UpdateTechDetails()
        ClearSelections()
    End Sub
    Private Sub PreQuoteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PreQuoteToolStripMenuItem.Click
        ChangeDisposition(listInactiveRepairs.SelectedItem, PreQuoteToolStripMenuItem.ToString())
        RefreshForm()
        UpdateTechDetails()
        ClearSelections()
    End Sub
    Private Sub UnitRepairToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UnitRepairToolStripMenuItem.Click
        ChangeDisposition(listInactiveRepairs.SelectedItem, UnitRepairToolStripMenuItem.ToString())
        RefreshForm()
        UpdateTechDetails()
        ClearSelections()
    End Sub
    Private Sub ChangeDisposition(SerialNumber As String, NewStatus As String)
        Using db As New ProductionDataModelDataContext
            Dim SelectedRepair = db.Repairs.Where(Function(t) t.SerialNumber = SerialNumber).First
            If NewStatus = "Unit Repair" Then
                SelectedRepair.EndTime = Nothing
                SelectedRepair.Status = NewStatus
            Else
                SelectedRepair.Status = NewStatus
            End If

            db.SubmitChanges()
        End Using
    End Sub
    Public Function TotalRepairs(TechnicianID As String, SelectedDate As DateTime, Status As String) As Integer
        Using db As New ProductionDataModelDataContext
            Dim QueryTotalRepairs = From t In db.Repairs
                                    Where t.EndTime.Value.Date = SelectedDate.Date _
                                    And t.Status = Status _
                                    And t.TechnicianID = TechnicianID _
                                    Select t.SerialNumber
            Return QueryTotalRepairs.Count
        End Using
    End Function
End Class
 
Given the comments in your code, the number of methods seems reasonable. The thing is, in a well architected project, a lot of that functionality shouldn't actually be in the form. You may well need to have a handler for an event of a Button or menu item but that method would then just invoke a method of another object to do the work.

When considering architecture, the Single Responsibility Principle should be something held to the fore of your mind. The responsibility of a form is to provide a point of interface between your app and the user. It's not the form's responsibility to perform complex processing. That should fall to objects purpose built for that single responsibility.

Windows Forms can be a bit kludgy in this regard, which is why WPF was built from the ground up with this in mind. WPF data-binding goes well beyond what Windows Forms offers and makes even event handlers redundant. The MVVM pattern is generally employed in WPF projects where a view model is bound to the UI and data flows automatically between the two. The view model reacts to any changes in the data and performs any first-level processing, passing off anything more complex to other objects built for the purpose.
 
Back
Top