troubleshooting Question

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

Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland asked on
Programming Languages-OtherVisual Basic Classic
4 Comments1 Solution299 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Carl Tawn
Senior Systems and Integration Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros