delete text file from Listbox selection... vb.net 2003

Hi Experts,

I've written a small winform app in vb.net 2003... The idea is to type into textbox and click stamp button.. the text entry appears into a Listbox and a text file with name from the textbox is created. If the Listbox Item is selected... it will delete the text file from its location.

1. Type a word into Textbox1
2. click Button1
3. Textbox1 value appears in Listbox1
4. A file is created 'c:\Jobs\Textbox name.txt'

1. Listbox1 Item is selected
2. Label99.Text = ListBox1.SelectedItem
3. Button2 is clicked & text file  'c:\Jobs\Textbox name.txt' is deleted!

I know my code sucks... I'm still only p/t with vb. I'm sure I'm over compensating.

Most of the code works except the delete text file part. Can an expert please advise were I'm going wrong. Please see my code.

Cheers,
Roberto
Imports System.IO

 Dim FileToDel As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'click button and text in textbox1 appears in Listbox1 as well as creates a txt file 'textbox1.data.text'!      
 
If TextBox1.Text = "" Then
            MsgBox("OI")
        Else
            ListBox1.Items.Add(TextBox1.Text)

            Try
                Dim iWsave As System.IO.StreamWriter
                iWsave = File.AppendText("C:\Users\bob\Documents\" & TextBox1.Text & ".txt")
                iWsave.WriteLine(TextBox1.Text & ";" & ComboBox1.Text & ";" & ComboBox2.Text)
                iWsave.Close()
            Catch ex As Exception
                MsgBox("Error code 10, Contact your Administrator!")
            End Try
        End If

        TextBox1.Text = ""
        ComboBox1.Text = "P?"
        ComboBox2.Text = " ..."
    End Sub



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Select Listbox1 item, not only removes the items but should delete the txt file too!

        Do Until ListBox1.SelectedItems.Count = 0
            REM  ListBox2.Items.Add(ListBox1.Items(ListBox1.SelectedIndices(0)))
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(0))
        Loop

        FileToDel = "C:\Users\bob\Documents\" & Label99.Text & ".txt"

        If System.IO.File.Exists(FileToDel) = True Then
            System.IO.File.Delete(FileToDel)
            MsgBox("File Deleted")
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Label99.Text = ListBox1.SelectedItem
    End Sub
End Class

Open in new window

RobertoFreemanoAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Not still 100% clear, but I think you want something along the lines of:
        For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1

            FileToDel = "C:\Users\bob\Documents\" + ListBox1.Items(ListBox1.SelectedIndices(i)) + ".txt"

            If System.IO.File.Exists(FileToDel) = True Then
                System.IO.File.Delete(FileToDel)
                MsgBox("File Deleted")
            End If

            ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))

        Next

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
It looks like you are allowing multiple items to be selected and removed at once, is that the case?

If so then you will need to do the delete as part of your loop, and use the value from the selected item rather than from the label in order to delete the correct file.
0
 
RobertoFreemanoAuthor Commented:
The idea is to type text into multiple textboxes/comboboxes... however, all that data is written to 1 single textfile file = "textbox1.name.text', which is saved to c:\ when Button1 is clicked. The text from Textbox1 registers in Listbox.

If Listbox Item is selected & If Button2 is cliicked, it simply removes the item from the Listbox & the text file from c:\

Hope that makes more sense.
0
 
RobertoFreemanoAuthor Commented:
Thanks carl_tawn

That did the trick ;)

Roberto
0
All Courses

From novice to tech pro — start learning today.