Get value of all items checked

Hi, I have a listbox where i manually populated with some values. How do i check which ones are selected and then write those values to a text file using a stream writer. So far i am unsuccessfully, i only get the last value selected !
Here is my code so far:

Under Form load:

  lboSearches.Items.Add("Tax Search")
        lboSearches.Items.Add("Fire Report")
        lboSearches.Items.Add("Sewer Search")
        lboSearches.Items.Add("Certificate of Occupancy")
        lboSearches.Items.Add("Covenants and Restrictions")
        lboSearches.Items.Add("Last Deed of Record")
        lboSearches.Items.Add("Highway Violations")
        lboSearches.Items.Add("Emergency Repairs")
        lboSearches.Items.Add("Housing and Building")
        lboSearches.Items.Add("Judgements & Liens, ect.")
        lboSearches.Items.Add("Underwriter Inserts")
        lboSearches.Items.Add("Sidewalk Violation")
        lboSearches.Items.Add("Miscellaneous")
        lboSearches.Items.Add("Parking Violation")
        lboSearches.Items.Add("Foreclosure")
        lboSearches.Items.Add("Street Report")
        lboSearches.Items.Add("Survey Print")
        lboSearches.Items.Add("Bankruptcy Search")
        lboSearches.Items.Add("Patriot Search")



Under Command Button that should get the values and write them to text file called 123.txt

Private Sub cmdCopyAFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopyAFiles.Click
        Dim bd, cd, ld, od, sd As String ' archive directories
        Dim destination As String 'destination directory
        bd = "C:\scans\belle\archive\"
        cd = "C:\scans\cg\archive\"
        ld = "C:\scans\la\archive\"
        od = "C:\scans\ocean\archive\"
        sd = "C:\scans\sa\archive\"
        destination = "C:\Documents and Settings\Administrator.BELLEABSTRACT\Desktop\tops scans\" 'destination on desktop
        Dim ttr As String
        Dim type As String
        If chkOcean.Checked = True Then
            ttr = "OA-" & txtTitleNo.Text.ToUpper
        ElseIf chkBelle.Checked = True Then
            ttr = "BELLE-" & txtTitleNo.Text.ToUpper
        ElseIf chkSA.Checked = True Then
            ttr = "SA-" & txtTitleNo.Text.ToUpper
        ElseIf chkCG.Checked = True Then
            ttr = "CG-" & txtTitleNo.Text.ToUpper
        ElseIf chkLA.Checked = True Then
            ttr = "LA-" & txtTitleNo.Text.ToUpper
        ElseIf chkBelle27.Checked = True Then
            ttr = "BELLE-" & txtTitleNo.Text.ToUpper
        ElseIf chkBelle37.Checked = True Then
            ttr = "BELLE-" & txtTitleNo.Text.ToUpper
        End If
        MsgBox(ttr)

        Dim fulltitle As String
        Dim dirs As String

        If ttr.StartsWith("CG-") = True Then
            dirs = cd
            MsgBox(dirs)
        ElseIf ttr.StartsWith("BELLE-") = True Then
            dirs = bd
            MsgBox(dirs)
        ElseIf ttr.StartsWith("SA-") = True Then
            dirs = sd
            MsgBox(dirs)
        ElseIf ttr.StartsWith("LA-") = True Then
            dirs = ld
            MsgBox(dirs)
        ElseIf ttr.StartsWith("OA-") = True Then
            dirs = od
            MsgBox(dirs)
        End If
        Dim str As String
        File.Delete("C:\123.txt")
        Dim sw As New StreamWriter("c:\123.txt", True)
        Dim stype = lboSearches.SelectedItem

       
        Try
            For Each stype In Me.lboSearches.SelectedItem
                sw.WriteLine(stype)

            Next
        Catch ex As Exception
            Throw New System.Exception("ERROR could not execute commands", ex)
        End Try
        sw.Close()

        fulltitle = dirs & ttr & type & ".tif"
        ' If File.Exists(fulltitle) = False Then
        'MsgBox("File named " & fulltitle & " does not exist!", MsgBoxStyle.Information, "FILE DOES NOT EXIST")
         
    End Sub
csakg6s0Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

tpwellsCommented:
This should work

        Dim fs As New FileStream(filename, FileMode.Create, FileAccess.Write)
        Dim sw As New StreamWriter(fs)
        For Each obj As Object In lboSearches.SelectedItems
            sw.WriteLine(obj.ToString)
        Next
        sw.Close()
        fs.Close()
Fernando SotoRetiredCommented:
Hi csakg6s0;

Replace this code in your program

        Dim stype = lboSearches.SelectedItem

       
        Try
            For Each stype In Me.lboSearches.SelectedItem
                sw.WriteLine(stype)

            Next
        Catch ex As Exception

        Dim stype As Object

With this code:

        Try
            For Each stype In Me.lboSearches.SelectedItems
                sw.WriteLine(CType(stype, String))
            Next
        Catch ex As Exception


Fernando
Fernando SotoRetiredCommented:
Sorry that should read

With this code:

        Dim stype As Object

        Try
            For Each stype In Me.lboSearches.SelectedItems
                sw.WriteLine(CType(stype, String))
            Next
        Catch ex As Exception

Fernando
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

csakg6s0Author Commented:
This is my code now:

Dim bd, cd, ld, od, sd As String ' archive directories
        Dim destination As String 'destination directory
        bd = "C:\scans\belle\archive\"
        cd = "C:\scans\cg\archive\"
        ld = "C:\scans\la\archive\"
        od = "C:\scans\ocean\archive\"
        sd = "C:\scans\sa\archive\"
        destination = "C:\Documents and Settings\Administrator.BELLEABSTRACT\Desktop\tops scans\" 'destination on desktop
        Dim ttr As String
        Dim type As String
        If chkOcean.Checked = True Then
            ttr = "OA-" & txtTitleNo.Text.ToUpper
        ElseIf chkBelle.Checked = True Then
            ttr = "BELLE-" & txtTitleNo.Text.ToUpper
        ElseIf chkSA.Checked = True Then
            ttr = "SA-" & txtTitleNo.Text.ToUpper
        ElseIf chkCG.Checked = True Then
            ttr = "CG-" & txtTitleNo.Text.ToUpper
        ElseIf chkLA.Checked = True Then
            ttr = "LA-" & txtTitleNo.Text.ToUpper
        ElseIf chkBelle27.Checked = True Then
            ttr = "BELLE-" & txtTitleNo.Text.ToUpper
        ElseIf chkBelle37.Checked = True Then
            ttr = "BELLE-" & txtTitleNo.Text.ToUpper
        End If
        MsgBox(ttr)

        Dim fulltitle As String
        Dim dirs As String

        If ttr.StartsWith("CG-") = True Then
            dirs = cd
            MsgBox(dirs)
        ElseIf ttr.StartsWith("BELLE-") = True Then
            dirs = bd
            MsgBox(dirs)
        ElseIf ttr.StartsWith("SA-") = True Then
            dirs = sd
            MsgBox(dirs)
        ElseIf ttr.StartsWith("LA-") = True Then
            dirs = ld
            MsgBox(dirs)
        ElseIf ttr.StartsWith("OA-") = True Then
            dirs = od
            MsgBox(dirs)
        End If
        Dim str As String
        File.Delete("C:\123.txt")
        Dim fs As New FileStream("C:\123.txt", FileMode.Create, FileAccess.Write)
        Dim sw As New StreamWriter(fs)


        Dim stype As Object

        Try
            For Each stype In Me.lboSearches.SelectedItems
                sw.WriteLine(CType(stype, String))
            Next
        Catch ex As Exception
        End Try

        sw.Close()
        fs.Close()

and i'm still just getting only the "LAST" value written to the text file
Fernando SotoRetiredCommented:
What is the value of the ListBox SelectionMode Property?

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
csakg6s0Author Commented:
That was it , it was on one, not multiple simple!!
Fernando SotoRetiredCommented:
Very good. Glad I was able to help. :=)
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
Visual Basic.NET

From novice to tech pro — start learning today.