Remove Items Fom ListBox: How To?

Palehorse
09-17-2004, 04:39 PM
Programmers,

I have a problem, I have a ini that has a bunch of numbers listed in it like so:

123
456
789

At form load, this data is populated in a listbox. You can select multiple numbers at a time in the listbox. While these numbers are selected, I click on my "Remove" button, and it should remove the selected items off the listbox. The ini file is left alone. This is my progress, however it is flawed:

Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click

Try
Dim aryListSelected As String
Dim arySelected() As String
Dim intNames As Integer

If lstNames.SelectedItems.Count > 0 Then
For Each strSeleted As String In lstNames.SelectedItems
If Len(aryListSelected) > 0 Then
aryListSelected &= mySeperator
End If
aryListSelected &= lstNames.SelectedIndex.ToString
Next strSeleted
arySelected = Split(aryListSelected, mySeperator)
End If

For intLoop As Integer = 1 To intNames
lstNames.Items.RemoveAt(Val(arySelected(intLoop - 1)))
Next intLoop

Catch ex As Exception
Call MessageBox.Show(ex.Message, Windows.Forms.Application.ProductName, MessageBoxButtons.OK)
End Try

End Sub

Palehorse
09-17-2004, 06:29 PM
I think I got it, let me know though. Also, if I wanted to, how would I make the ini file reflect the changes?

While (lstNames.SelectedIndices.Count > 0)
lstNames.Items.RemoveAt(lstNames.SelectedIndex)
End While

MKoslof
09-18-2004, 09:56 AM
That method will work fine. Regarding the ini file, you are going to have to write back to the file :). If you want to remove a line or add a line, you can open a new streamwriter or textwriter, or a new streamreader or textreader and copy over the file, append to the file, read in the values and do something, etc.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum