ListView Control (sorting and indexes) 
Author Message
 ListView Control (sorting and indexes)

Maybe this will save someone else some trouble (or maybe I should have
figured this out long ago).

I was having trouble with the ListView, it's index and the
SelectedItem.  I wanted to set the first item in the list to be the
the SelectedItem after I populated the list.  For some reason the
first item I added to the list (which ended up being the forth item)
was always getting set as the SelectedItem.

It turns out that I was doing the sort after I was attempting to
select the first item.  If I did the sort first and then the following
line

Set lvAppList.SelectedItem = lvAppList.ListItems(1)

then the first item in the list was selected.

I guess that the Index changes when ever the list is sorted
differently???



Sat, 27 Nov 2004 03:47:34 GMT  
 ListView Control (sorting and indexes)
Using the code below, I can't reproduce the problem... are you using API's
to sort?
'================
Option Explicit

Private Sub Form_Load()
   Dim i As Integer
   With ListView1
      .View = lvwReport
      .FullRowSelect = True
      .ColumnHeaders.Add , , "Text"
      .ColumnHeaders.Add , , "Sub1"
      .ColumnHeaders.Add , , "Sub2"
      For i = 1 To 10
         With .ListItems.Add
            .Text = "Item " & i
            With .ListSubItems.Add
               .Text = "Sub1 " & i
            End With
            With .ListSubItems.Add
               .Text = "Sub2 " & i
            End With
         End With
      Next
   End With
End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader _
                        As MSComctlLib.ColumnHeader)
   With ListView1
      .SortOrder = Abs(.SortOrder - 1) 'Toggles
      .SortKey = ColumnHeader.Index - 1
      .Sorted = True
      Set .SelectedItem = .ListItems(1) 'Always Top Item
   End With

End Sub
'================

--
Ken Halter
MS-MVP-VB
http://www.vbsight.com/
Please keep it in the groups..

Quote:

> Maybe this will save someone else some trouble (or maybe I should have
> figured this out long ago).

> I was having trouble with the ListView, it's index and the
> SelectedItem.  I wanted to set the first item in the list to be the
> the SelectedItem after I populated the list.  For some reason the
> first item I added to the list (which ended up being the forth item)
> was always getting set as the SelectedItem.

> It turns out that I was doing the sort after I was attempting to
> select the first item.  If I did the sort first and then the following
> line

> Set lvAppList.SelectedItem = lvAppList.ListItems(1)

> then the first item in the list was selected.

> I guess that the Index changes when ever the list is sorted
> differently???



Sat, 27 Nov 2004 11:07:54 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. ListView Item Index after manual sort

2. Listview API sort and changed indexes

3. Listview add index and retrieve index?

4. Get items from Listview sorted with API:s in sorted order

5. SORRY, Sorting listview controls :(

6. Sorting a listview control in details view

7. Sorting ListView control on other columns

8. How Can I perform sorting with ListView control?

9. Sorting in Listview control

10. Q: How to sort by date in ListView Control

11. Sorting numeric data in a ListView control?

12. Sorting a column in a listview control

 

 
Powered by phpBB® Forum Software