Solved

ListView Sort problem

Posted on 2001-06-15
6
733 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Ark,
How to get the data from listview (Not VB ListItem, OK) after API sort.
plz, Help Me. Very urgent

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Accepted Solution

by:
Ark earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now