Results 1 to 3 of 3

Thread: ListView - Sorting: Does index # change

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2000
    Posts
    17
    I have a ListView in my project which populates with with all zip files in a selected directory. The user can change the order (ascending/descending) on either column.
    Q1...I wish to always have the first file in the list (when reordered) selected. I've tried:
    lstMyListView.ListItems(0).Selected = True
    but that doesn't work.
    Q2...Even the item selected isn't always highlighted, why?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    The index will ofcourse change but not the key or text or any other property
    lstMyListView.ListItems(1).Selected = True
    shoud do the trick since listitems is option base 1.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Guest
    Start a new project, add a listview and paste following code. NOTE:this is only sample code. Use any of it when needed, throw the rest overboard

    Enjoy!

    Code:
    Private Sub Form_Load()
        Dim I As ListItem
        Dim T As Integer
        
        With ListView1
            
            ' Set some properties
            .AllowColumnReorder = False ' User cannot re-arrange the columns
            .FullRowSelect = True       ' Select complete rows, not just a "cell"
            .GridLines = True           ' Add gridlines
            .HideSelection = False      ' Make sure item is highlighted when lost focus
            .LabelEdit = lvwManual      ' User cannot edit values in columns
            .MultiSelect = False        ' User cannot select multiple rows
            .View = lvwReport           ' View-type
            
            ' Add two columns
            .ColumnHeaders.Add , , "Item Name"
            .ColumnHeaders.Add , , "Item Prize"
            
            ' Right Align Column 2
            .ColumnHeaders(2).Alignment = lvwColumnRight
            
            ' Fill up the list with random data
            For T = 1 To 10
                Set I = .ListItems.Add(, "KEY" & CStr(T), "Item - " & CStr(T))
                I.SubItems(1) = Format(Rnd * 1000, "#0.00")
            Next T
        
            ' Select Item 1
            Set ListView1.SelectedItem = ListView1.ListItems(1)
        End With
        
    End Sub
    
    Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
        With ListView1
            ' Sort ascending or descending?
            If .SortOrder = lvwDescending Then .SortOrder = lvwAscending Else .SortOrder = lvwDescending
            
            ' Which column is the sortkey?
            .SortKey = ColumnHeader.Index - 1
            
            ' Set Sorted to True to start sorting
             .Sorted = True
            
            ' Select Item 1
            Set ListView1.SelectedItem = ListView1.ListItems(1)
        End With
    End Sub

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