I have a text file which contains the data of an image (path, latitude and longitude). the textfile is place in a listview and the data in textfile is place in textboxes. if i click one item in listview then click save. it will create an image containing the data in the text file, the problem is if the textfile contains a 20,000 rows then call by the system. you need to click all the data in rows one by one and click save one by on, not user friendly and too risky.

now my Questions is:

Is it possible to save all item in a listview in a single click of button. Here is my code for saving the all item in listview.

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnsave.Click

        Dim n As Integer

        'line to save the specific file
        For n = 0 To LV.Items.Count + 1 ' <--- tthis line create  more than one image


            If EW.Copyright <> txtcopyright.Text Then
                EW.Copyright = txtcopyright.Text
                Update = True
            End If
            If EW.Description <> txtdescription.Text Then
                EW.Description = txtdescription.Text
                Update = True
            End If
            EW.GPSLatitude = lat
            Update = True
            EW.GPSLongitude = Lon

            Update = True

            EW.GPSLatitudeRef = LatRef
            EW.GPSLongitudeRef = LonRef



            If Update Then
                'Always update last mofified date
                ' EW.DateTimeLastModified = Now
                'Get the updated image
                Dim newImage As Bitmap = EW.GetBitmap
                Dim Fi As IO.FileInfo = New IO.FileInfo(LV.SelectedItems(0).Text)
                'Calculate a new filename
                Dim NewFileName As String
                Dim I As Integer = 0
                Do
                    I += 1
                    NewFileName = txtpath.Text & " (" & I.ToString & ")"
                    txtpath.Text = NewFileName
                    NewFileName = IO.Path.Combine(Fi.DirectoryName, NewFileName & Fi.Extension)
                Loop Until Not IO.File.Exists(NewFileName)
                'Save the updated image file.
                Select Case Fi.Extension.ToUpper
                    Case ".PNG"
                        newImage.Save(NewFileName, Drawing.Imaging.ImageFormat.Png)
                    Case ".JPG"
                        ' JPG requires a trick for lossless saving
                        Dim NewFilenameTemp = NewFileName & ".temp"
                        ' We rotate the image for 90 degrees aand save to temp 
                        Dim Enc As Encoder = Encoder.Transformation
                        Dim EncParm As EncoderParameter
                        Dim EncParms As New EncoderParameters(1)
                        EncParm = New EncoderParameter(Enc, CLng(EncoderValue.TransformRotate90))
                        EncParms.Param(0) = EncParm
                        Dim CodecInfo As ImageCodecInfo = GetEncoderInfo("image/jpeg")
                        ' Write the rotated image with new properties
                        newImage.Save(NewFilenameTemp, CodecInfo, EncParms)

                        ' Release memory
                        newImage.Dispose()
                        newImage = Nothing
                        GC.Collect()

                        ' Delete the original file
                        System.IO.File.Delete(NewFileName)

                        ' Rotate back and save as new
                        newImage = Image.FromFile(NewFilenameTemp)
                        EncParm = New EncoderParameter(Enc, CLng(EncoderValue.TransformRotate270))
                        EncParms.Param(0) = EncParm
                        newImage.Save(NewFileName, CodecInfo, EncParms)

                        ' Release memory
                        newImage.Dispose()
                        newImage = Nothing
                        GC.Collect()

                        ' Delete the temporary file
                        System.IO.File.Delete(NewFilenameTemp)
                    Case Else
                        MsgBox("Can't save file types " & Fi.Extension)
                End Select
            End If
        Next n
    End Sub

Any Comments,Suggestion and idea that may help will highly appriciated.. Thanks in Advanced..

jonel

You will greatly sacrfice performace by iterating through 20k records.

If you are worried about duplicates, just remove the item saved from the listview. (Keep them from clicking it again)

If you are still wanting to save everything, just iterate through and pull in values you want.

Hi Thanks for reply,

i don't have an idea on how to save multiple file, can you give one(codes) so that i will study and apply to my system.

by the way, i am a new to programming..

Jonel-

For method one above, something like this may work:

'Class level variable
Dim lviCurrent As ListViewItem

Private Sub ListView1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ListView1.Click
    Try
        If Not IsNothing(Me.ListView1.SelectedItems) Then
            If ListView1.SelectedItems.Count = 1 Then
                lviCurrent = ListView1.SelectedItems(0)
            Else
                'Build in referencing for multiple items?
                Exit Sub
            End If
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
    Try

        'Your update Code

        If Not IsNothing(lviCurrent) Then ListView1.Items.Remove(lviCurrent)
        lviCurrent = Nothing
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

For the second method listed above, you can iterate through each item:

For Each lvi As ListViewItem in ListView1.Items
    'Place your update code here referencing lvi    
Next

ListView1.Items.Clear()

Edited 3 Years Ago by Begginnerdev

This article has been dead for over six months. Start a new discussion instead.