I have a project where I collect plays for a game, and store the details of the play in a listbox /collection. Later, I want to be able to obtain the last play and reverse it, then delete it from the listbox / collection.
My problem is the delete of the listbox works, but the delete of the collection does not work.
Attached is an abbreviated version of the project.
What am I doing wrong?
What I have tried:
The form contains a
listbox
(
ListEntries
), two buttons (
btnAdd
and
bthUndo
), and a
textbox
(
TxtDisplay
)
btnAdd
adds a generated entry to the
listbox
and collection, then displays the result in the
textbox
.
Each time it is clicked, it adds another entry to the
listbox
/ collection.
btnUndo
is supposed to return the LAST entry in the collection, display it, then delete it from the
listbox
and the collection. Each time it is clicked, it should delete the last entry in the list.
The problem is that the “
return
” always returns the LAST entry added – period. The
listbox
entry is deleted, but it appears the collection entry is never deleted. What am I doing wrong.
Class to contain details of each play.
Public Class CEntry
Public EntrySeq As Integer
Public EntryTxt As String
Public Sub add()
EntrySeq = 0
EntryTxt = ""
End Sub
Public Overrides Function ToString() As String
Dim display As String
display = "Play=" + EntrySeq.ToString("D2") +
" EntryText=" + EntryTxt
Return display
End Function
End Class
Class to contain collection of plays.
Public Class CCollect
Inherits CollectionBase
Public Sub add(ByRef newentry As CEntry)
Me.List.Add(newentry)
End Sub
End Class
Form1
Routines
Public Class Form1
Dim AddCount As Integer
Dim SelectIndex As Integer
Dim SelectSub As Integer
Dim Entry As New CEntry
Dim Collect As New CCollect
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
AddCount += 1
Entry.EntrySeq = AddCount
Entry.EntryTxt = "S" + AddCount.ToString("D2")
Collect.add(Entry)
SelectIndex = ListEntries.Items.Add(Entry)
TxtDisplay.Text = "Count=" + Collect.Count.ToString("D2") +
" Entry=" + Entry.ToString
End Sub
Private Sub bthUndo_Click(sender As Object, e As EventArgs)
Handles bthUndo.Click
Dim testSelection As New CEntry
Dim SelectIndex As Integer
Dim SelectSub As Integer
SelectSub = Collect.Count
SelectIndex = SelectSub - 1
If SelectSub = 0 Then
Exit Sub
End If
testSelection = Nothing
testSelection = Collect(SelectIndex)
TxtDisplay.Text = "Count=" + SelectSub.ToString("D2") +
" Entry=" + testSelection.ToString
ListEntries.Items.RemoveAt(SelectIndex)
Collect.RemoveAt(SelectIndex)
End Sub
End Class