Hack Forums
[.NET] VB.NET SORT LISTVIEW - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] VB.NET SORT LISTVIEW (/showthread.php?tid=5844044)

Pages: Pages 1 2


VB.NET SORT LISTVIEW - markzuckerberg - Jun 3, 2018

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!  🤝


RE: VB.NET SORT LISTVIEW - CodeAssassin - Jun 3, 2018

Change For each to a For loops with a counter added in to count to 15 or on the 15th irritation. jump out.


RE: VB.NET SORT LISTVIEW - HFDone - Jun 3, 2018

use ListView1.Items.Count


RE: VB.NET SORT LISTVIEW - markzuckerberg - Jun 3, 2018

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


RE: VB.NET SORT LISTVIEW - HFDone - Jun 3, 2018

in your for loop before you add the items put If ListView1.Items.Count = 15 Then Exit For


RE: VB.NET SORT LISTVIEW - markzuckerberg - Jun 3, 2018

(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 ?


RE: VB.NET SORT LISTVIEW - HFDone - Jun 3, 2018

For Each file In files
If ListView1.Items.Count = 15 Then Exit For
With LVMAINFILEMANAGER
.Items.Add((file).ToString())
...


RE: VB.NET SORT LISTVIEW - Dalvik - Jun 3, 2018

(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?


RE: VB.NET SORT LISTVIEW - HFDone - Jun 3, 2018

(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."


RE: VB.NET SORT LISTVIEW - Dalvik - Jun 3, 2018

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


RE: VB.NET SORT LISTVIEW - HFDone - Jun 3, 2018

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


RE: VB.NET SORT LISTVIEW - markzuckerberg - Jun 4, 2018

(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 ?


RE: VB.NET SORT LISTVIEW - HFDone - Jun 4, 2018

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())
...


RE: VB.NET SORT LISTVIEW - markzuckerberg - Jun 4, 2018

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


RE: VB.NET SORT LISTVIEW - HFDone - Jun 4, 2018

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/system.io.filesysteminfo.lastwritetime(v=vs.110).aspx