Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

VB.NET SORT LISTVIEW

Hello HF,

I need help here.. I have a list view with items(Files) added from a directory and sub directories as below code :

Code
Dim directory = "c:\"
       Dim files() As System.IO.FileInfo
       Dim dirinfo As New System.IO.DirectoryInfo(directory)
       files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
       For Each file In files
           With LVMAINFILEMANAGER
               .Items.Add((file).ToString())
               With .Items(.Items.Count - 1).SubItems
                   .Add((file.DirectoryName).ToString())
                   .Add((file.CreationTime).ToString())
               End With
           End With
           LVMAINFILEMANAGER.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
           LVMAINFILEMANAGER.Columns(1).Width = "0"
       Next

       On Error Resume Next

Now i need the Listview to show only last 15 files that added or modified files, i don't want to add them all in the listview.  🤨

can it be done ?  🤦‍♂

Thanks in advance!  🤝
Change For each to a For loops with a counter added in to count to 15 or on the 15th irritation. jump out.
use ListView1.Items.Count
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jun 3, 2018 04:23 AM)CodeAssassin Wrote:  Change For each to a For loops with a counter added in to count to 15 or on the 15th irritation. jump out.

I'm not that professional, is there any example ?


(Jun 3, 2018 04:28 AM)HFDone Wrote:  use ListView1.Items.Count
I always get errors when working with Listview.
in your for loop before you add the items put If ListView1.Items.Count = 15 Then Exit For
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jun 3, 2018 04:41 AM)HFDone Wrote:  in your for loop before you add the items put If ListView1.Items.Count = 15 Then Exit For

Any example ?
For Each file In files
If ListView1.Items.Count = 15 Then Exit For
With LVMAINFILEMANAGER
.Items.Add((file).ToString())
...
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jun 3, 2018 08:02 AM)HFDone Wrote:  For Each file In files
  If ListView1.Items.Count = 15 Then Exit For
  With LVMAINFILEMANAGER
      .Items.Add((file).ToString())
      ...

This works if he only wants to display 15 items in total. What if he wants to run whatever he is doing again without clearing previous results?
(Jun 3, 2018 08:41 AM)Dalvik Wrote:  This works if he only wants to display 15 items in total. What if he wants to run whatever he is doing again without clearing previous results?

"Now i need the Listview to show only last 15 files that added or modified files, i don't want to add them all in the listview."
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jun 3, 2018 08:48 AM)HFDone Wrote:  "Now i need the Listview to show only last 15 files that added or modified files, i don't want to add them all in the listview."

I know. Read what I said again.
(Jun 3, 2018 08:51 AM)Dalvik Wrote:  I know. Read what I said again.

he didnt mention it so he should decide if he want to clear to listview b4 or to do some math.
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jun 3, 2018 08:02 AM)HFDone Wrote:  For Each file In files
  If ListView1.Items.Count = 15 Then Exit For
  With LVMAINFILEMANAGER
      .Items.Add((file).ToString())
      ...

This codes works for me and shows 15 item, but not the last modified or added (not the latest files that been added).

thanks in advance, any help ?
if i get you
Dim Num = ListView1.Items.Count + 15
For Each file In files
If ListView1.Items.Count = Num Then Exit For
With LVMAINFILEMANAGER
.Items.Add((file).ToString())
...
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Jun 4, 2018 08:59 AM)HFDone Wrote:  if i get you
Dim Num = ListView1.Items.Count + 15
For Each file In files
 If ListView1.Items.Count = Num Then Exit For
 With LVMAINFILEMANAGER
     .Items.Add((file).ToString())
     ...

Thanks for your fast reply,

Nothing changed.. Same results still getting a random files not the latest added or modified files.
to get latest added or modified files you need to use FileInfo Class and to check the file LastWriteTime property and to compare it to current time.
https://msdn.microsoft.com/en-us/library....110).aspx
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by SoldiercrimesMar 22, 2018 05:27 AM 3 184 Mar 22, 2018 05:27 AM
Last Post: Ninja
[C] by SigmaMay 2, 2017 03:54 PM 7 180 May 2, 2017 03:54 PM
Last Post: Sigma
[.NET] by xVipXApr 2, 2015 04:54 AM 3 250 Apr 2, 2015 04:54 AM
Last Post: xVipX