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?
 
Fernando SotoRetiredCommented:
What is the value of the ListBox SelectionMode Property?
0
 
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()
0
 
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
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
0
 
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
0
 
csakg6s0Author Commented:
That was it , it was on one, not multiple simple!!
0
 
Fernando SotoRetiredCommented:
Very good. Glad I was able to help. :=)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.