dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    5

    ImageList with icons and sorting...

    Hey guys...

    I hope this is the right Area... In my company, the decision was made that with Access no more applications are developed. So we have to create Windows Applications...

    So far - so good. But i have a problem and i can't resolve it...

    I have a Image List and i want to show all Files in a directory in it. With Application icon an after double-click the assigned app should open the file...

    This works - perfectly...

    But i want to sort the files. Key is the creationTime. The oldest File should be last in the list.

    Tried many things but nothing really worked...

    It looks like this:

    Name:  menu.JPG
Views: 73
Size:  23.3 KB

    Here is my Code:

    Code:
     Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim parts() As String = Split(My.User.Name, "\")
            Dim username As String = parts(1)
    
            lblUser.Text = "Angemeldet als " & username
    
            ListView1.LargeImageList = ImageList1
            ListView1.SmallImageList = ImageList1
            ListView1.View = System.Windows.Forms.View.List
    
            Dim SuchPfad As String
            Dim dirInfo As DirectoryInfo
            Dim fileInfo As FileInfo
            Dim exeIcon As System.Drawing.Icon
            SuchPfad = "C:\Test\Daten"
            dirInfo = New DirectoryInfo(SuchPfad)
            LabelOrdner.Text = SuchPfad
    
    
            For Each fileInfo In dirInfo.GetFiles
                If (Not String.IsNullOrEmpty(fileInfo.Extension)) Then
    
                    exeIcon = System.Drawing.Icon.ExtractAssociatedIcon(fileInfo.FullName)
    
                    If (ImageList1.Images.ContainsKey(fileInfo.FullName)) Then
                        ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
                        ListView1.Items.Add(fileInfo.CreationTime)
                    ElseIf (Not exeIcon Is Nothing) Then
                        ImageList1.Images.Add(fileInfo.FullName, exeIcon)
                        With ListView1.Items.Add(fileInfo.Name, fileInfo.FullName)
                        End With
                    Else
                        ListView1.Items.Add(fileInfo.Name)
                    End If
                End If
            Next
        End Sub
    
        Public Function GetAssociatedProgram(ByVal FileExtension As _
           String) As String
            ' Returns the application associated with the specified
            ' FileExtension
            ' ie, path\denenv.exe for "VB" files
            Dim objExtReg As Microsoft.Win32.RegistryKey = _
                 Microsoft.Win32.Registry.ClassesRoot
            Dim objAppReg As Microsoft.Win32.RegistryKey = _
                Microsoft.Win32.Registry.ClassesRoot
            Dim strExtValue As String
            Try
                ' Add trailing period if doesn't exist
                If FileExtension.Substring(0, 1) <> "." Then _
                    FileExtension = "." & FileExtension
                ' Open registry areas containing launching app details
                objExtReg = objExtReg.OpenSubKey(FileExtension.Trim)
                strExtValue = objExtReg.GetValue("").ToString
                objAppReg = objAppReg.OpenSubKey(strExtValue & _
                                "\shell\open\command")
                ' Parse out, tidy up and return result
                Dim SplitArray() As String
                SplitArray = Split(objAppReg.GetValue(Nothing).ToString, """")
                If SplitArray(0).Trim.Length > 0 Then
                    Return SplitArray(0).Replace("%1", "")
                Else
                    Return SplitArray(1).Replace("%1", "")
                End If
            Catch
                Return ""
            End Try
        End Function
    
        Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
            ToolStripProgressBar1.Value = 0
            ToolStripStatusLabel1.Text = ""
        End Sub
    
        Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
            My.Computer.Audio.Play(My.Resources.schrei1, AudioPlayMode.Background)
            Process.Start(LabelOrdner.Text & "\" & (ListView1.FocusedItem.Text))
        End Sub
    I hope you can help me. I'm good in VBA, VB.Net is kind of similar but i'm really nooby in this.


    For later:
    I want to make a funktion of it - but this is the next step... Is it possible to create a function like: ShowFolder("Path", "ImageListName") and then the Listbox gets filled?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,914

    Re: ImageList with icons and sorting...

    vb.net Code:
    1. For Each fileInfo In dirInfo.GetFiles().OrderByDescending(Function(fi) fi.CreationTime)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    5

    Re: ImageList with icons and sorting...

    Yesss! Thanks! It's working!

    Your are the Best!

    But can you explain me, whats happening there?

    I found OrderByDescending by myself but nothing to sort by FileInfo. Where does "Function(fi)" comes from? Is that integrated?
    Last edited by Toddel; Feb 22nd, 2018 at 06:46 AM.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,914

    Re: ImageList with icons and sorting...

    OrderBy and OrderByDescending are LINQ extension methods. You can call them on any list that implements the IEnumerable(Of T) interface. DirectoryInfo.GetFiles returns a FileInfo array, which does implement that interface, with T being FileInfo. The argument expected by OrderBy and OrderByDescending is a delegate, i.e. a reference to a method. That method must be a function that has a parameter of type T, i.e. it takes an item from the list as an argument, and it must return the value that will be used to represent that item in the sort. In this example, the delegate provided is a Lambda expression, which is what usually gets used in such cases for simplicity. Like extension methods, Lambda expressions are a language feature that was introduced specifically to support LINQ. You could use a regular named method instead though, which may make things slightly easier to understand. If you were to write this method:
    Code:
    Private Function GetFileInfoSortKey(fi As FileInfo) As Date
        Return fi.CreationTime
    End Function
    then you could use it instead of a Lambda expression:
    vb.net Code:
    1. For Each fileInfo In dirInfo.GetFiles().OrderByDescending(AddressOf GetFileInfoSortKey)
    A Lambda expression is just an anonymous method. I've highlighted the parts of the named method that you need to reproduce in the Lambda expression. Everything else is inferred from usage.

    You might need to do some more reading on generics and LINQ to fully understand this but that's basically the way of it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    5

    Re: ImageList with icons and sorting...

    Thank you very much...

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.