Results 1 to 4 of 4

Thread: Bowling Application

  1. #1

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Bowling Application

    Below is the code for a bowling program that I have developed for our bowling club.
    The idea is you enter a members name which appears in the members list, clicking on a name in the members list puts that name in the first available Mat if no mats are available the name goes in to the current player list. When a game has finished you click Change players button and the players are switched with the ones in the current player list. You can add / delete members, Add / Remove Mat3 and 4 and remove a players name .
    I have posted a list of names which you can copy / paste in to the collection property of lstMembers for testing. I did zip it but it's just over the allowance for posting.
    It works and does all I require of it, it would be nice to get feedback on how I've coded it.

    Code:
    Option Explicit On
    
    Public Class Form1
        Private Const APP_NAME As String = "SaveRestoreListBox"
        Private Const SECTION_NAME As String = "Items"
        Dim clearMat4 = False, clearMat3 = False
        ' Load saved items from the Registry.
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
        e As System.EventArgs) Handles MyBase.Load
            lstHidden.Items.Clear()
    
            Dim i As Integer = 0
            Do
                Dim new_value As String = GetSetting(APP_NAME,
                        SECTION_NAME, "Item" & i.ToString())
                If new_value.Length = 0 Then Exit Do
    
                lstHidden.Items.Add(new_value)
                lstMembers.Items.Add(new_value)
                i += 1
            Loop
        End Sub
        ' Save the current items.
        Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
        As System.ComponentModel.CancelEventArgs) Handles _
        MyBase.Closing
            ' Delete existing items.
            ' Catch errors in case the section doesn't exist.
            Try
                DeleteSetting(APP_NAME, SECTION_NAME)
            Catch ex As Exception
            End Try
    
            ' Save the items.
            For i As Integer = 0 To lstHidden.Items.Count - 1
                SaveSetting(APP_NAME, SECTION_NAME, "Item" &
                i.ToString(), lstHidden.Items(i).ToString())
            Next i
        End Sub
        Private Sub BtnMat1_Click(sender As Object, e As EventArgs) Handles BtnMat1.Click
            'switch 4 players from current list to mat 1 and place the players from mat 1 back to current player list
            Static num = 3
            For clearLb = 0 To 3
                lstNames.Items.Add(LstMat1.Items(num))
                LstMat1.Items.Remove(LstMat1.Items(num))
                num = num - 1
                If num = -1 Then
                    For fillListbox = 0 To 3
                        LstMat1.Items.Insert(0, lstNames.Items(0))
                        lstNames.Items.Remove(lstNames.Items(0))
                        num = 3
                    Next
                End If
            Next
        End Sub
    
        Private Sub BtnMat2_Click(sender As Object, e As EventArgs) Handles BtnMat2.Click
            'switch 4 players from current list to mat 2 and place the players from mat 2 back to current player list
            Static num = 3
            For clearLb = 0 To 3
                lstNames.Items.Add(LstMat2.Items(num))
                LstMat2.Items.Remove(LstMat2.Items(num))
                num = num - 1
                If num = -1 Then
                    For fillListbox = 0 To 3
                        LstMat2.Items.Insert(0, lstNames.Items(0))
                        lstNames.Items.Remove(lstNames.Items(0))
                        num = 3
                    Next
                End If
            Next
        End Sub
    
        Private Sub BtnMat3_Click(sender As Object, e As EventArgs) Handles BtnMat3.Click
            'switch 4 players from current list to mat 3 and place the players from mat 3 back to current player list
            Static num = 3
            For clearLb = 0 To 3
                lstNames.Items.Add(LstMat3.Items(num))
                LstMat3.Items.Remove(LstMat3.Items(num))
                num = num - 1
                If num = -1 Then
                    For fillListbox = 0 To 3
                        LstMat3.Items.Insert(0, lstNames.Items(0))
                        lstNames.Items.Remove(lstNames.Items(0))
                        num = 3
                    Next
                End If
            Next
        End Sub
        Private Sub BtnMat4_Click(sender As Object, e As EventArgs) Handles BtnMat4.Click
            'switch 4 players from current list to mat 4 and place the players from mat 4 back to current player list
            Static num = 3
            For clearLb = 0 To 3
                lstNames.Items.Add(LstMat4.Items(num))
                LstMat4.Items.Remove(LstMat4.Items(num))
                num = num - 1
                If num = -1 Then
                    For fillListbox = 0 To 3
                        LstMat4.Items.Insert(0, lstNames.Items(0))
                        lstNames.Items.Remove(lstNames.Items(0))
                        num = 3
                    Next
                End If
            Next
        End Sub
        Private Sub btnRemovePlayer_Click(sender As Object, e As EventArgs) Handles btnRemovePlayer.Click
            If lstNames.Items.Count <= 0 Then
                MessageBox.Show("There are no players to remove",
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1)
                Exit Sub
            Else
                lstMembers.Items.Add(lstNames.SelectedItem) ' return to members list
                lstNames.Items.Remove(lstNames.SelectedItem)  'remove player from current player list
            End If
        End Sub
        Private Sub btnRemove4_Click(sender As Object, e As EventArgs) Handles btnRemove4.Click
            If clearMat3 = True Then
                MessageBox.Show("Play Mat 3 before Mat 4",
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1)
                Exit Sub
            End If
            If clearMat4 = False Then
                LstMat4.Enabled = False
                LstMat4.Visible = False
                Label4.Visible = False
                BtnMat4.Visible = False
                btnRemove4.Text = "Play Mat 4"
                If LstMat4.Items.Count <> 0 Then
                    For i = LstMat4.Items.Count - 1 To 0 Step -1
                        lstNames.Items.Add(LstMat4.Items(i))
                    Next
                End If
                LstMat4.Items.Clear()
                clearMat4 = True
            Else
                If lstNames.Items.Count < 4 Then
                    MessageBox.Show("You need at least 4 players",
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1)
                    Exit Sub
                End If
                LstMat4.Enabled = True
                LstMat4.Visible = True
                Label4.Visible = True
                BtnMat4.Visible = True
                btnRemove4.Text = "Remove Mat 4"
                Static num = 3
                For clearLb = 0 To 3
                    num = num - 1
                    If num = -1 Then
                        For fillListbox = 0 To 3
                            LstMat4.Items.Insert(0, lstNames.Items(0))
                            lstNames.Items.Remove(lstNames.Items(0))
                            num = 3
                        Next
                    End If
                Next
                clearMat4 = False
            End If
        End Sub
    
        Private Sub lstMembers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstMembers.SelectedIndexChanged
            Dim listBoxes = {LstMat1, LstMat2, LstMat3, LstMat4, lstNames}.Where(Function(lb) lb.Enabled).ToArray()
            Dim listBox = listBoxes(0)
    
            For i = 1 To listBoxes.GetUpperBound(0)
                If listBox.Items.Count = 4 Then
                    listBox = listBoxes(i)
                Else
                    Exit For
                End If
            Next
            If lstMembers.SelectedIndex <> -1 Then
                listBox.Items.Add(lstMembers.SelectedItem)
                lstMembers.Items.RemoveAt(lstMembers.SelectedIndex)
            End If
    
        End Sub
    
        Private Sub btnDeleteMember_Click(sender As Object, e As EventArgs) Handles btnDeleteMember.Click
            Dim strMatch = InputBox("Delete Member", "Delete Member").Trim
            For Each itm In lstMembers.Items
                If String.Equals(itm, strMatch, StringComparison.CurrentCultureIgnoreCase) Then ' if they match
                    lstMembers.Items.Remove(itm) ' remove from members list
                    lstHidden.Items.Remove(itm) ' remove from hidden list
                    Exit For
                End If
            Next
        End Sub
    
        Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
            Dim new_item As String = InputBox("Enter New Members name", "New " &
        "Member")
            If new_item.Length = 0 Then Exit Sub
            lstHidden.Items.Add(new_item)
            lstMembers.Items.Add(new_item)
        End Sub
    
        Private Sub btnRemove3_Click(sender As Object, e As EventArgs) Handles btnRemove3.Click
            If clearMat4 = False Then
                MessageBox.Show("Mat 4 is still in play!",
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1)
                Exit Sub
            End If
            If clearMat3 = False Then
                LstMat3.Enabled = False
                LstMat3.Visible = False
                Label3.Visible = False
                BtnMat3.Visible = False
                btnRemove3.Text = "Play Mat 3"
                If LstMat3.Items.Count <> 0 Then
                    For i = LstMat3.Items.Count - 1 To 0 Step -1
                        lstNames.Items.Add(LstMat3.Items(i))
                    Next
                End If
                LstMat3.Items.Clear()
                clearMat3 = True
            Else
                If lstNames.Items.Count < 4 Then
                    MessageBox.Show("You need at least 4 players",
                    "Important Note",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1)
                    Exit Sub
                End If
                LstMat3.Enabled = True
                LstMat3.Visible = True
                Label3.Visible = True
                BtnMat3.Visible = True
                btnRemove3.Text = "Remove Mat 3"
                Static num = 3
                For clearLb = 0 To 3
                    num = num - 1
                    If num = -1 Then
                        For fillListbox = 0 To 3
                            LstMat3.Items.Insert(0, lstNames.Items(0))
                            lstNames.Items.Remove(lstNames.Items(0))
                            num = 3
                        Next
                    End If
                Next
                clearMat3 = False
            End If
        End Sub
    
        Private Sub LstMat1_Click(sender As Object, e As EventArgs) Handles LstMat1.Click
            ' remove a single player from LstMat1
            Dim delete As String
            If LstMat1.Items.Count > 0 Then
                delete = MessageBox.Show("Delete" & vbCrLf & LstMat1.SelectedItem & "?",
                    "Important Note",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1)
                If delete = vbYes Then ' check this is the player you want to remove
                    lstMembers.Items.Add(LstMat1.SelectedItem) 'put the player back in to lstMembers
                    LstMat1.Items.Remove(LstMat1.SelectedItem)  ' and remove from lstMat1
                    If lstNames.Items.Count <> 0 Then               ' if lstNames has players
                        LstMat1.Items.Insert(0, lstNames.Items(0)) 'place first name in lstNames in to lstMat1
                        lstNames.Items.Remove(lstNames.Items(0))   ' remove first name from lstNames
                    Else                                            ' if no names in lstNames
                        For i = LstMat1.Items.Count - 1 To 0 Step -1 ' then place players from lstMat1 
                            lstMembers.Items.Add(LstMat1.Items(i))  ' back in to lstMembers (lstMats need 4 players)
                        Next
                        LstMat1.Items.Clear()
                    End If
                End If
            End If
        End Sub
    
    End Class
    NAMES
    Graham
    Anne
    Marion
    Mick
    Jan
    Pauline
    John
    Eiry
    Mandy
    Arwel
    Iris
    Malc
    Pat
    Kath
    Barbara
    Rhona
    Doreen
    Ann
    Learning is a never ending subject.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: Bowling Application

    You understandably have some repetition in your code due to having 4 mats, a good example of this is BtnMat1_Click, BtnMat2_Click, etc... which all have virtually identical code.

    That kind of situation is a prime candidate for creating your own Sub's, where you can put the repeated code, and just pass parameters for the parts that change.

    I must confess I haven't fully understood the purpose of the Static variable num, but as the only things that vary in your code for BtnMat1_Click etc are num and the LstMat1 etc, the sub (and usage) in this case could be like this:
    Code:
        Private Sub SwitchToMat(ByRef num as Integer, ListBoxForMat as ListBox)
            'switch 4 players from current list to the specified mat and place the players from the specified mat back to current player list
            For clearLb = 0 To 3
                lstNames.Items.Add(ListBoxForMat.Items(num))
                ListBoxForMat.Items.Remove(ListBoxForMat.Items(num))
                num = num - 1
                If num = -1 Then
                    For fillListbox = 0 To 3
                        ListBoxForMat.Items.Insert(0, lstNames.Items(0))
                        lstNames.Items.Remove(lstNames.Items(0))
                        num = 3
                    Next
                End If
            Next
        End Sub
    
        Private Sub BtnMat1_Click(sender As Object, e As EventArgs) Handles BtnMat1.Click
            Static num = 3
            SwitchToMat(num, LstMat1)
        End Sub
    
        Private Sub BtnMat2_Click(sender As Object, e As EventArgs) Handles BtnMat2.Click
            Static num = 3
            SwitchToMat(num, LstMat2)
        End Sub
        ...
    Making this change not only reduces the size of your code, but also means that if you need to make changes later (possibly due to a bug fix, or a change in requirements), you only need to change it in one place.

    Quote Originally Posted by Mallard8 View Post
    I did zip it but it's just over the allowance for posting.
    Unless your program has lots of picture files or similar it doesn't need to be that large, I suspect the majority of the size is from the Bin and Obj folders, which you should remove.

    Not only are they automatically generated by Visual Studio when required (so we don't need them), but more importantly if you have any malware on your computer then the files in those folders could pass the malware on to us.
    Last edited by si_the_geek; Dec 12th, 2018 at 07:53 AM.

  3. #3

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: Bowling Application

    Thanks for looking Si,
    I knew there was repetitive code but wasn't sure how to go about cutting it down, I do like what you have suggested and I'll change my code accordingly.
    In future I'll also remove the bin and obj folders.
    Learning is a never ending subject.

  4. #4

    Thread Starter
    Hyperactive Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    284

    Re: Bowling Application

    Just noticed I have a sub to remove a single player from Mat1, I also want to do this for Mat2 ,Mat3 and Mat4 so an ideal opportunity to have a go at creating my own sub to cut down on the repeats.
    Learning is a never ending subject.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width