Find & Remove from List?

yogi_bear_79

Member
Joined
Sep 30, 2009
Messages
19
Programming Experience
Beginner
I have a simple class called Player. I can add players to it, but haven't figured out how to remove them. I've tried various ways, some compile some don't none work

VB.NET:
Public Class Player

    Public Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub
End Class

examples of how I am adding:
VB.NET:
players.Add(New Player(txtNewPlayer.Text))
players.Add(New Player(PlayerArchiveList.SelectedItem))

basically what I want to do is remove PlayerArchiveList.SelectedItem from archivePlayers
 
Then that's exactly what you should do:
VB.NET:
players.Remove(PlayerArchiveList.SelectedItem)
More correctly that should be:
VB.NET:
players.Remove(DirectCast(PlayerArchiveList.SelectedItem, Player))
 
I had tired that before. Double checked again, but I get the following error at runtime: Unable to cast object of type 'System.String' to type 'manger.Player'
 
Ah OK, I see now that you have actually added Strings to your ListBox rather than adding the Player objects themselves. Assuming there isn't a reason you can't, I think that it would be preferable to create the Player objects and actually add them to the ListBox rather than just the names. Would that be appropriate or not?
 
The only reason I can see that I would need the strings is because I read/write to an .csv file with the names. With that said I would think they would need to be converted from object to strings at some point?

This is how a user can add a new player. Basically the new player is first added to the player.csv file, then added to the PlayerlList (listbox). The player is also added to the List players
VB.NET:
Public Class frmAddNewPlayer

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim x As Long
        Dim sSearch As String
        Dim match As Boolean = False
        Dim fs As New FileStream(FilePath, FileMode.Append, FileAccess.Write) '...declare filestream w/write access
        Dim s As New StreamWriter(fs) '...create streamwriter filestream object fs as argument

        s.WriteLine(txtNewPlayer.Text)
        s.Close()

        For x = 0 To frmPlayers.PlayerList.Items.Count - 1
            sSearch = LCase(frmPlayers.PlayerList.Items.Item(x))
            If sSearch = LCase(txtNewPlayer.Text) Then
                MsgBox("Player already entered into tournament.", MsgBoxStyle.Critical, "Player Manager")
                match = True
            End If

        Next

        If match = False Then
            frmPlayers.PlayerList.Items.Add(txtNewPlayer.Text)
            players.Add(New Player(txtNewPlayer.Text))
        End If

        
        Me.Close()
    End Sub

snippit: PlayerArchiveList and the List archivePlayers are populted froom the players.csv file
VB.NET:
 While s.Peek > -1
                sTxt = s.ReadLine()
                PlayerArchiveList.Items.Add(sTxt)
                archivePlayers.Add(New Player(sTxt))
            End While

Snippit from forms load event. populates the two listboxes from the Lists player and archive Player
VB.NET:
If players.Count > 0 Then
            For x = 0 To players.Count - 1
                obj = players.Item(x)
                PlayerList.Items.Add(obj.Name)
            Next
        End If

        If archivePlayers.Count > 0 Then
            For x = 0 To archivePlayers.Count - 1
                obj = archivePlayers.Item(x)
                PlayerArchiveList.Items.Add(obj.Name)
            Next
        End If

Finally: I have the two lists side by side with two buttons between << & >> to move players to the signed up list from the archive list and vice-versa. This last bit of code iw where my problem is. I want to be able to add/remove players from their respective lists, so later when the lists are used they are accurate.

VB.NET:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        If Not PlayerArchiveList.SelectedItem = "" Then
            PlayerList.Items.Add(PlayerArchiveList.SelectedItem)
            players.Add(New Player(PlayerArchiveList.SelectedItem))

            archivePlayers.Remove(PlayerArchiveList.SelectedItem)
            PlayerArchiveList.Items.RemoveAt(PlayerArchiveList.SelectedIndex)
        End If
        
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        If Not PlayerList.SelectedItem = "" Then
            PlayerArchiveList.Items.Add(PlayerList.SelectedItem)
            archivePlayers.Add(New Player(PlayerList.SelectedItem))

            players.Remove(PlayerArchiveList.SelectedItem)
            PlayerList.Items.RemoveAt(PlayerList.SelectedIndex)
        End If
        
    End Sub
 
Ok, i removed the only line of code that I used to add strings to my Listboxes

VB.NET:
frmPlayers.PlayerList.Items.Add(txtNewPlayer.Text)

Instead:

VB.NET:
If match = False Then
            players.Add(New Player(txtNewPlayer.Text))
            frmPlayers.PlayerList.Items.Clear() '...clear signed in players listbox
        End If

        '...populate listbox of signed in players
        If players.Count > 0 Then
            For x = 0 To players.Count - 1
                obj = players.Item(x)
                frmPlayers.PlayerList.Items.Add(obj.Name)
            Next
        End If

However this still fails, trying to convert a string to an object?

VB.NET:
If Not PlayerArchiveList.SelectedItem = "" Then
       players.Add(New Player(PlayerArchiveList.SelectedItem))
       archivePlayers.Remove(PlayerArchiveList.SelectedItem)'...code fails here at runtime
End If
 
To add player object to Listbox:
VB.NET:
frmPlayers.PlayerList.Items.Add(players(x))
Override the ToString method to return the display string, for example Name property. Or as alternative set DisplayMember, but you really have to define a Property for this.
 
What I would suggest is binding the list to the control, so there's only one list. For that you'll need to adjust your Player definition a bit:
VB.NET:
Public Class Player

    Private _name As String

    Public Property Name() As String
        Get
            Return Me._name
        End Get
        Set(ByVal value As String)
            Me._name = value
        End Set
    End Property

    Public Sub New(ByVal name As String)
        Me._name = name
    End Sub

End Class
Now you can bind your list to the ListBox like so:
VB.NET:
myBindingSource.DataSource = myList

myListBox.DisplayMember = "Name"
myListBox.DataSource = myBindingSource
Obviously you need to add a BindingSource to the form for that. Now, when you add a new Player to the list, you call ResetBindings on the BindingSource and the ListBox will update automatically. To remove the selected item you should be able to just call RemoveCurrent on the BindingSource.
 
Back
Top