[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ListView Sort problem

Posted on 2001-06-15
6
Medium Priority
?
747 Views
Last Modified: 2010-05-18
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

0
Comment
Question by:anij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 6197376
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
0
 

Author Comment

by:anij
ID: 6197521
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
0
 

Author Comment

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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Accepted Solution

by:
Ark earned 400 total points
ID: 6197573
Hi
You can use LVM_GETITEM = (LVM_FIRST + 5) message:

Public Function ListView_GetItem(lvhWnd As Long, pitem As LVITEM) As Boolean
  ListView_GetItem = SendMessage(lvhWnd, LVM_GETITEM, 0, pitem)
End Function
where pitem structure defined as follow:

' LVM_GET/SETITEM lParam
Public Type LVITEM   ' was LV_ITEM
  mask As Long
  iItem As Long
  iSubItem As Long
  state As Long
  stateMask As Long
  pszText As Long  ' if String, must be pre-allocated  before filled
  cchTextMax As Long
  iImage As Long
  lParam As Long
#If (WIN32_IE >= &H300) Then
  iIndent As Long
#End If
End Type

But more easy is to fill ALL items and subitems with strings without using API, adding spaces before numerig items:
was:
1
2
3
11
22
111
222
etc.
In this case vb listview sort items as following:
1,11,111,2,22,222,3.
But if you add spaces (note that space have ASCII = 32< any numeric value):
  1
  2
  3
 11
 22
111
222
vb ListView sort it like 1,2,3,11,22,111,222

For example, if your numeric values don't exeed 10 digits
Instead of ListView1.ListItems.Add myValue
use
ListView1.ListItems.Add Space(10 - Len(cStr(myVal))) & cStr(myVal)

Cheers


0
 

Author Comment

by:anij
ID: 6197727
Ark Thanx
I will check it.
You not checked this link.
http://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
0
 

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question