Link to home
Start Free TrialLog in
Avatar of anij
anij

asked on

ListView Sort problem

Hi Experts
I using ListView for listing my data. There is some alphabetic & numeric field.
To sort numeric field I used the following code.Yes it sort accordingly.
BUT THE PROBLEM IS AFTER SORT THIS NUMERIC FIELD I USED FOR LOOP TO GET THE DATA FROM LISTVIEW BUT IT NOT GETTING AS SHOWN IN LISTVIEW.
how can I get that same from  listview (there is no problem for alphabetic feild)

'for getting data from listview, it is not same as shown 'in listview after we click numeric field header
For I = 1 To .ListItems.Count
   strAPPNO = .ListItems(I).ListSubItems(2).Text
   strAPPNO = strAPPNO & Space(10 - Len(strAPPNO))
   Msgbox strAPPNO
Next

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
sOrder = Not sOrder
ListView1.SortKey = ColumnHeader.Index - 1
SortItemIdx = ColumnHeader.Index - 1

    Select Case ColumnHeader.Index - 1
       Case 1, 2, 3:
             ListView1.SortKey = SortItemIdx
             ListView1.SortOrder = Abs(Not ListView1.SortOrder = 1)
             ListView1.Sorted = True
       Case 4:
             ListView1.Sorted = False
             SendMessage ListView1.hWnd, LVM_SORTITEMS, ListView1.hWnd, ByVal PROC(AddressOf CompareValues)
    End Select
End Sub

Public Function CompareValues(ByVal lParam1 As Long, _
                              ByVal lParam2 As Long, _
                              ByVal hWnd As Long) As Long
     

   Dim val1 As Long
   Dim val2 As Long
     
   val1 = ListView_GetItemValueStr(hWnd, lParam1)
   val2 = ListView_GetItemValueStr(hWnd, lParam2)
     
   Select Case sOrder
      Case True: 'sort descending
           
            If val1 < val2 Then
                  CompareValues = 0
            ElseIf val1 = val2 Then
                  CompareValues = 1
            Else: CompareValues = 2
            End If
     
      Case Else: 'sort ascending
   
            If val1 > val2 Then
                  CompareValues = 0
            ElseIf val1 = val2 Then
                  CompareValues = 1
            Else: CompareValues = 2
            End If
   
   End Select

End Function

Public Function ListView_GetItemValueStr(hWnd As Long, lParam As Long) As Long

   Dim hIndex As Long
   Dim r As Long
 

   objFind.flags = LVFI_PARAM
   objFind.lParam = lParam
   hIndex = SendMessage(hWnd, LVM_FINDITEM, -1, objFind)
     

   objItem.mask = LVIF_TEXT
   objItem.iSubItem = SortItemIdx
   objItem.pszText = Space$(32)
   objItem.cchTextMax = Len(objItem.pszText)
   r = SendMessage(hWnd, LVM_GETITEMTEXT, hIndex, objItem)
   If r > 0 Then
      ListView_GetItemValueStr = CLng(Left$(objItem.pszText, r))
   End If

End Function

Public Function FARPROC(ByVal pfn As Long) As Long
  FARPROC = pfn
End Function

Avatar of Ark
Ark
Flag of Russian Federation image

Hi
 VB listView control sort items in alphabetical order only, so numeric values sorting like this:
1
11
2 etc
Once you're using API to sort values, you have to continue ALL processe with API, because API sort items in listview, but not in VB ListItems collection.
Instead of using API, you can make numeric values "right justified" before sorting:

For I = 1 To .ListItems.Count
  strAPPNO = .ListItems(I).ListSubItems(2).Text
  .ListItems(I).ListSubItems(2).Text = Space(10 - Len(strAPPNO)) & strAPPNO
Next

In this case all numeric items will sort correctly and you'll have access to ListItems collection.

Cheers
Avatar of anij
anij

ASKER

Ark
Tell me How to change VB ListItem or How can get data from
ListView (API)
I think
plz, Help me ASAP. I am in very urgent
Avatar of anij

ASKER

Hi Ark,
How to get the data from listview (Not VB ListItem, OK) after API sort.
plz, Help Me. Very urgent

ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anij

ASKER

Ark Thanx
I will check it.
You not checked this link.
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20096375
There I asked whether we can create our process as child process of another process ?
Is possible ? I will ask a valuable question.
plz, respond
Avatar of anij

ASKER

This is another link for the same problem.
It is looks like ur answer.
I will give u point after check it
OK
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10170794