delete text file from Listbox selection... 2003

Hi Experts,

I've written a small winform app in 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.

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 ''!      
If TextBox1.Text = "" Then

                Dim iWsave As System.IO.StreamWriter
                iWsave = File.AppendText("C:\Users\bob\Documents\" & TextBox1.Text & ".txt")
                iWsave.WriteLine(TextBox1.Text & ";" & ComboBox1.Text & ";" & ComboBox2.Text)
            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)))

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

        If System.IO.File.Exists(FileToDel) = True Then
            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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
RobertoFreemanoAuthor Commented:
The idea is to type text into multiple textboxes/comboboxes... however, all that data is written to 1 single textfile file = "', 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.
Carl TawnSystems 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
                MsgBox("File Deleted")
            End If



Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobertoFreemanoAuthor Commented:
Thanks carl_tawn

That did the trick ;)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.