' 1)Remove a selected Item from the ListBox myListBox.Items.Remove(myListBox.SelectedItem) ' 2)Remove a selected Item from the Generic List myDataList.Remove(DirectCast(myListBox.SelectedItem, myDataObject))
' STEP2: Remove a selected Item from the Generic List and also the ListBox Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' 1)Remove a selected Item from the ListBox Dim index As Integer = myListBox.SelectedIndex myListBox.Items.RemoveAt(index) ' 2)Remove a selected Item from the Generic List myDataList.RemoveAt(index) End Sub
'effacement myIndex = 1 'récupération de l'objet par la méthode find Dim myObject As myDynamicData = myDataList.Find(AddressOf findObject) 'effacement dans la listbox myListBox.Items.RemoveAt(myIndex) 'effacement dans la list(of...) myDataList.Remove(myObject)
Private Function findObject(ByVal t As myDynamicData) As Boolean If t.Data1 = myIndex Then Return True Else Return False End Function
Private myIndex As Integer
Public Overrides Function ToString(ByVal t As myDynamicData) As String Dim myLine As String myLine = t.Data1 & "," & t.Data2 & "," & t.Data3 Return myLine End Function
myDataList.ToString.Remove(2) 'Pour enlever la ligne 3 ???
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic Class Form1 Private myDataList As New List(Of myDataObject) Private myCounter As Integer ' STEP1: Add a list in the Generic List and in the ListBox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click myCounter = myCounter + 1 If myCounter > 12 Then Exit Sub myListBox.Items.Clear() ' Reset the ListBox myDataList.Add(New myDataObject(myCounter, "String" & Chr(64 + myCounter), "String2")) myDataList.ForEach(AddressOf addingObjectInListBox) End Sub ' STEP2: Remove a selected Item from the Generic List and also the ListBox Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' 1)Remove a selected Item from the ListBox myListBox.Items.Remove(myListBox.SelectedItem) ' 2)Remove a selected Item from the Generic List Dim obj As myDataObject = myDataList.Item(myListBox.SelectedIndex).ToString() ' <= ??? myDataList.Remove(obj) End Sub ' STEP3: Clear the ListBox Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click myListBox.Items.Clear() End Sub ' STEP4: Reload the Generic List in the ListBox Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click myDataList.ForEach(AddressOf addingObjectInListBox) End Sub Private Sub addingObjectInListBox(ByVal t As myDataObject) myListBox.Items.Add(t.Data1 & " " & t.Data2) End Sub ' Function 'ToString' cannot be declared 'Overrides' because it does not override a function in a base class Public Overloads Function ToString(ByVal t As myDataObject) As String Dim myLine As String myLine = t.Data1 & "," & t.Data2 & "," & t.Data3 Return myLine End Function End Class Public Class myDataObject Public Data1 As Integer Public Data2 As String Public Data3 As String Public Sub New(ByVal m_Data1 As Integer, ByVal m_Data2 As String, ByVal m_Data3 As String) Data1 = m_Data1 Data2 = m_Data2 Data3 = m_Data3 End Sub End Class
Public Overrides Function ToString() As String Dim myLine As String myLine = Data1 & "," & Data2 & "," & Data3 Return myLine End Function
myDataList.Remove(DirectCast(myListBox.SelectedItem, myDataObject))
myListBox.Items.Add(t.Data1 & " " & t.Data2)
myDataList.Remove(DirectCast(myListBox.SelectedItem, myDataObject))
myListBox.Items.Remove(myListBox.SelectedItem)
Public Class Form1 Private myDataList As New List(Of myDataObject) Private myCounter As Integer ' STEP1: Add a list in the Generic List and in the ListBox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click myCounter = myCounter + 1 If myCounter > 12 Then Exit Sub myListBox.Items.Clear() ' Reset the ListBox myDataList.Add(New myDataObject(myCounter, "String" & Chr(64 + myCounter)))', "String2")) myDataList.ForEach(AddressOf addingObjectInListBox) End Sub ' STEP2: Remove a selected Item from the Generic List and also the ListBox Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' 1)Remove a selected Item from the ListBox myListBox.Items.Remove(myListBox.SelectedItem) ' 2)Remove a selected Item from the Generic List myDataList.Remove(DirectCast(myListBox.SelectedItem, myDataObject)) End Sub ' STEP3: Clear the ListBox Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click myListBox.Items.Clear() End Sub ' STEP4: Reload the Generic List in the ListBox Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click myDataList.ForEach(AddressOf addingObjectInListBox) End Sub Private Sub addingObjectInListBox(ByVal t As myDataObject) myListBox.Items.Add(t.Data1 & " " & t.Data2) End Sub End Class Public Class myDataObject Public Data1 As Integer Public Data2 As String 'Public Data3 As String Public Sub New(ByVal m_Data1 As Integer, ByVal m_Data2 As String) ', ByVal m_Data3 As String) Data1 = m_Data1 Data2 = m_Data2 ' Data3 = m_Data3 End Sub Public Overrides Function ToString() As String Dim myLine As String myLine = Data1 & " " & Data2 '& " " & Data3 Return myLine End Function End Class