Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27201

VS 2010 [RESOLVED] Remove several items from a list (of t)

$
0
0
I have the following code which takes the selected items in a listbox and removes them from a list. As you can see, I call Dict.Remove for each individual selected object.
I feel there will be a "cleaner" way to do this using predicates or LINQ. I have tried searching the 'net but haven't found anything to do what I want. i.e. Dict.RemoveList(Listbox1.SelectedItems) type of thing.
vb.net Code:
  1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  2.         If ListBox1.SelectedIndices.Count > 0 Then
  3.             Dim sStr As String = "Are you sure you want to delete the" & vbCrLf & "following item(s) from the database?" & vbCrLf & vbCrLf
  4.             For Each obj As Object In ListBox1.SelectedItems
  5.                 sStr += obj.ToString & vbCrLf
  6.             Next
  7.             Dim Reply As System.Windows.Forms.DialogResult = MessageBox.Show(sStr, "Delete entries", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
  8.             If Reply = Windows.Forms.DialogResult.Yes Then
  9.                 'take the count of entries in Dict
  10.                 Dim iDictWords As Integer = 0
  11.                 iDictWords = Dict.Count
  12.                 'remove the lines from Dict
  13.                 For Each obj As Object In ListBox1.SelectedItems
  14.                     Dict.Remove(obj.ToString)
  15.                 Next
  16.                 'rewrite file to delete entries
  17.                 File.WriteAllLines(MyFile, Dict.ToArray)
  18.                 'TODO remove the items from the listbox list
  19.                 MsgBox((iDictWords - Dict.Count).ToString & " entries deleted")
  20.             End If
  21.         End If
  22.     End Sub

Viewing all articles
Browse latest Browse all 27201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>