Results 1 to 4 of 4

Thread: Help with listview items

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    14

    Help with listview items

    I am creating a program that allowes me to select items from a listview and save them in a .txt file.
    Code:
    Imports System.IO
    
    Public Class cv7import
    
        Private Sub cv7import_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim caminho As String
            caminho = "C:\Documents and Settings\Software\Ambiente de trabalho\cv7import"
    
            Dim returnValue As String()
            returnValue = Environment.GetCommandLineArgs()
            If returnValue.Length > 1 Then
                MessageBox.Show(returnValue(1).ToString())
            Else
                MessageBox.Show("Nothing")
            End If
    
            ' Set ListView Properties  
            lstvicon.View = View.Details
            lstvicon.GridLines = False
            lstvicon.FullRowSelect = True
            lstvicon.HideSelection = False
            lstvicon.MultiSelect = True
    
            ' Create Columns Headers  
            lstvicon.Columns.Add("Nome")
            lstvicon.Columns.Add("Extensão")
            lstvicon.Columns.Add("Tamanho")
            lstvicon.Columns.Add("Data Modificação")
    
            Dim DI As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(caminho)
    
            Dim files() As System.IO.FileInfo = DI.GetFiles
    
            Dim file As System.IO.FileInfo
    
            Dim li As ListViewItem
            For Each file In files
                li = lstvicon.Items.Add(file.Name)
                li.SubItems.Add(file.Extension)
                li.SubItems.Add(file.Length)
                li.SubItems.Add(file.LastWriteTimeUtc)
                'li.SubItems.Add(FileDialog)
            Next
    
    
    
        End Sub
    
        Private Sub btnimp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimp.Click
    
            ' Creates a csv File
            Dim csv As New System.IO.StreamWriter("C:\Documents and Settings\Software\Ambiente de trabalho\cv7import\teste.csv", True)
    
            lstvicon.SelectedItems.CopyTo(csv)
    
            csv.Close()
    
    
        End Sub
    End Class
    Can someone give me a push with this please?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with listview items

    try this:

    vb Code:
    1. Private Sub btnimp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimp.Click
    2.     Dim items() As ListViewItem = lstvicon.SelectedItems.Cast(Of ListViewItem).ToArray
    3.     Dim csv() As String = Array.ConvertAll(items, Function(lvi) String.Join(",", lvi.SubItems.Cast(Of ListViewItem.ListViewSubItem).Select(Function(si) si.Text).ToArray))
    4.     IO.File.WriteAllLines("C:\Documents and Settings\Software\Ambiente de trabalho\cv7import\teste.csv", csv)
    5. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    14

    Re: Help with listview items

    damn that works perfectly!! thanks a lot.

    Can you also help me on making the names be converted into short name? The format i really wanted was around this :

    movieprograms : moviep~1

    This being on the csv. Do you have any idea if this is possible?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with listview items

    here's an api function that gives you a short path from a file path:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Declare Function GetShortPathName Lib "kernel32" _
    4.                     Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
    5.                     ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim shortPath As New String(" "c, 256)
    9.         GetShortPathName("long path here", shortPath, 256)
    10.         MsgBox(shortPath.Trim)
    11.     End Sub
    12.  
    13. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width