Solved

ListView Sort problem

Posted on 2001-06-15
6
734 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
  • 4
  • 2
6 Comments
 
LVL 27

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Accepted Solution

by:
Ark earned 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now