Newsgroup: it.comp.lang.visual-basic
Titolo: list liew..

Come faccio ad ordinare un subitem in formato numerico o data correttamente
e non come fosse testo?
grazie a tutti!!



...
Prova con questo codice prelevato da Vbnet (chiedo scusa pre la lunghezza
del post)
            Visual Basic ListView API Routines
            Posted Saturday September 6, 1997
            Updated Tuesday September 01, 1998
            Sorting ListView ListItems Using CallbacksRelated Topics
              VBnet Frames
            Applies to: VB5
 Prerequisites
            Enhanced Comctl32 functionality is only available to users with
            comctl32.dll version 4.70 or greater installed. This dll is
            typically installed with IE3.x or IE4. See the Dev Centre for
            information on redistributing the 4.72 version of comctl32.dll.
Sorting numeric values with the listview control's built-in sort
            mechanism has always left much to be desired. Because the
listview
            stores the item and subitem data as strings, the default sorting
            algorithm sorted numbers in string format. This meant that
instead
            of a sort of 1, 2, 3, 20, 35, the listview sorted as 1, 2, 20,
3,
            35.
            Beginning with VB5, the AddressOf operator has finally allowed
            Visual Basic applications access to Windows callback
functionality
            without the need for third-party message interceptors or
subclassing
            agents.
            By utilizing AddressOf, SendMessage and the listview data
structures
            and messages, we now have the ability to create sorting routines
to
            react based on the type of data in a given listview item or
subitem
            column. This exercise will create such a listview, populate it
with
            strings, dates and values, and sort each correctly based on the
data
            type in the column. Once the basic principles are grasped, the
            mechanism to perform this type of sort can be moved into
virtually
            any listview application.
            This demo is based in part on a Knowledge Base article on
sorting
            dates. However, instead of using the VB's VarPtr() function,
I've
            created the routines to use the standard data types associated
with
            the listview. Those wanting further info on the VarPtr() method
            should search the Knowledge Base for article Q170884.
            The code below contains all the requirements to construct the
            illustration as shown. To a form, add a listview (ListView1), a
            picturebox (Picture1, used to output the "Print Out ListItems",
and
            three command buttons (cmdDisplay, cmdDisplayAPI and cmdEnd).
Labels
            are optional. Add a BAS module to the project as well.
Configuration
            of the listview's report view and columns-creation is done
through
            code.
 BAS Module Code
            Place the following code into the general declarations area of a
bas
            module:
Public objFind As LV_FINDINFO
Public objItem As LV_ITEM
'variable to hold the sort order (ascending or descending)
Public sOrder As Boolean
Public Type POINT
  x As Long
  y As Long
End Type
Public Type LV_FINDINFO
  flags As Long
  psz As String
  lParam As Long
  pt As POINT
  vkDirection As Long
End Type
Public Type LV_ITEM
    mask As Long
    iItem As Long
    iSubItem As Long
    state As Long
    stateMask As Long
    pszText As String
    cchTextMax As Long
    iImage As Long
    lParam As Long
    iIndent As Long
End Type
'Constants
Public Const LVFI_PARAM = 1
Public Const LVIF_TEXT = &H1
Public Const LVM_FIRST = &H1000
Public Const LVM_FINDITEM = LVM_FIRST + 13
Public Const LVM_GETITEMTEXT = LVM_FIRST + 45
Public Const LVM_SORTITEMS = LVM_FIRST + 48
'API declarations
Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA"
 _
  ByVal hWnd As Long, _
  ByVal wMsg As Long, _
  ByVal wParam As Long, _
  ByVal lParam As Long) As Long
Public Declare Function SendMessageAny Lib "user32" Alias "SendMessageA" ( _
  ByVal hWnd As Long, _
  ByVal wMsg As Long, _
  ByVal wParam As Long, _
  lParam As Any) As Long
Public Function CompareDates(ByVal lParam1 As Long, _
                             ByVal lParam2 As Long, _
                             ByVal hWnd As Long) As Long
  'CompareDates: This is the sorting routine that gets passed to the
  'ListView control to provide the comparison test for date values.
  'Compare returns:
  ' 0 = Less Than
  ' 1 = Equal
  ' 2 = Greater Than
   Dim dDate1 As Date
   Dim dDate2 As Date
  'Obtain the item names and dates corresponding to the
  'input parameters
   dDate1 = ListView_GetItemDate(lParam1, hWnd)
   dDate2 = ListView_GetItemDate(lParam2, hWnd)
  'based on the Public variable sOrder set in the
  'columnheader click sub, sort the dates appropriately:
   Select Case sOrder
      Case True:    'sort descending
            If dDate1 < dDate2 Then
                  CompareDates = 0
            ElseIf dDate1 = dDate2 Then
                  CompareDates = 1
            Else: CompareDates = 2
            End If
      Case Else: 'sort ascending
            If dDate1 > dDate2 Then
                  CompareDates = 0
            ElseIf dDate1 = dDate2 Then
                  CompareDates = 1
            Else: CompareDates = 2
            End If
   End Select
End Function
Public Function CompareValues(ByVal lParam1 As Long, _
                              ByVal lParam2 As Long, _
                              ByVal hWnd As Long) As Long
  'CompareValues: This is the sorting routine that gets passed to the
  'ListView control to provide the comparison test for numeric values.
  'Compare returns:
  ' 0 = Less Than
  ' 1 = Equal
  ' 2 = Greater Than
   Dim val1 As Long
   Dim val2 As Long
  'Obtain the item names and values corresponding
  'to the input parameters
   val1 = ListView_GetItemValueStr(hWnd, lParam1)
   val2 = ListView_GetItemValueStr(hWnd, lParam2)
  'based on the Public variable sOrder set in the
  'columnheader click sub, sort the values appropriately:
   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_GetItemDate(lParam As Long, hWnd As Long) As Date
   Dim r As Long
   Dim hIndex As Long
  'Convert the input parameter to an index in the list view
   objFind.flags = LVFI_PARAM
   objFind.lParam = lParam
   hIndex = SendMessageAny(hWnd, LVM_FINDITEM, -1, objFind)
  'Obtain the value of the specified list view item.
  'The objItem.iSubItem member is set to the index
  'of the column that is being retrieved.
   objItem.mask = LVIF_TEXT
   objItem.iSubItem = 1
   objItem.pszText = Space$(32)
   objItem.cchTextMax = Len(objItem.pszText)
  'get the string at subitem 1
   r = SendMessageAny(hWnd, LVM_GETITEMTEXT, hIndex, objItem)
  'and convert it into a date and exit
   If r > 0 Then
      ListView_GetItemDate = CDate(Left$(objItem.pszText, r))
   End If
End Function
Public Function ListView_GetItemValueStr(hWnd As Long, lParam As Long) As
Long
   Dim r As Long
   Dim hIndex As Long
  'Convert the input parameter to an index in the list view
   objFind.flags = LVFI_PARAM
   objFind.lParam = lParam
   hIndex = SendMessageAny(hWnd, LVM_FINDITEM, -1, objFind)
  'Obtain the value of the specified list view item.
  'The objItem.iSubItem member is set to the index
  'of the column that is being retrieved.
   objItem.mask = LVIF_TEXT
   objItem.iSubItem = 2
   objItem.pszText = Space$(32)
   objItem.cchTextMax = Len(objItem.pszText)
  'get the string at subitem 2
   r = SendMessageAny(hWnd, LVM_GETITEMTEXT, hIndex, objItem)
  'and convert it into a long
   If r > 0 Then
      ListView_GetItemValueStr = CLng(Left$(objItem.pszText, r))
   End If
End Function
'--end block--'
 Form Code
            Add a check button to the project (chkSelectMode) with the
caption
            "Use Full Row Select". Set its initial value to 0. Add the
following
            code to the check button sub:
Option Explicit
Private Sub Form_Load()
   Dim itmX As ListItem
   Dim itmH As ColumnHeader
  'Add three Column Headers to the control
   Set itmH = ListView1.ColumnHeaders.Add(Text:="Name")
   Set itmH = ListView1.ColumnHeaders.Add(Text:="Date")
   Set itmH = ListView1.ColumnHeaders.Add(Text:="Value")
  'Set the Listview to Report view
   ListView1.View = lvwReport
  'Add some data to the ListView control
   Set itmX = ListView1.ListItems.Add(Text:="Joe")
   itmX.SubItems(1) = "05/07/97"
   itmX.SubItems(2) = "44"
   Set itmX = ListView1.ListItems.Add(Text:="Sally")
   itmX.SubItems(1) = "04/08/93"
   itmX.SubItems(2) = "16"
   Set itmX = ListView1.ListItems.Add(Text:="Bill")
   itmX.SubItems(1) = "05/29/94"
   itmX.SubItems(2) = "1"
   Set itmX = ListView1.ListItems.Add(Text:="Fred")
   itmX.SubItems(1) = "03/17/95"
   itmX.SubItems(2) = "215"
   Set itmX = ListView1.ListItems.Add(Text:="Anne")
   itmX.SubItems(1) = "07/01/97"
   itmX.SubItems(2) = "20"
   Set itmX = ListView1.ListItems.Add(Text:="Bob")
   itmX.SubItems(1) = "04/01/91"
   itmX.SubItems(2) = "21"
   Set itmX = ListView1.ListItems.Add(Text:="John")
   itmX.SubItems(1) = "12/25/92"
   itmX.SubItems(2) = "176"
   Set itmX = ListView1.ListItems.Add(Text:="Paul")
   itmX.SubItems(1) = "11/23/95"
   itmX.SubItems(2) = "113"
   Set itmX = ListView1.ListItems.Add(Text:="Maria")
   itmX.SubItems(1) = "02/01/96"
   itmX.SubItems(2) = "567"
End Sub
Private Sub cmdDisplay_Click()
   Dim hIndex As Long
   Dim hItem As Long
   Picture1.Cls
  'loop trough the ListItems collection
   For hIndex = 1 To ListView1.ListItems.Count
     Picture1.Print ListView1.ListItems.Item(hIndex).Text,
     'for each item in the collection, loop through the subitems.
     'subitem 0 is the main item (Note the terminating comma!!)
      For hItem = 1 To 2
         Picture1.Print ListView1.ListItems.Item(hIndex).SubItems(hItem),
      Next hItem
     'move to the next line
      Picture1.Print
   Next hIndex
End Sub
Private Sub cmdDisplayAPI_Click()
   Dim r As Long
   Dim hIndex As Long
   Dim hItem As Long
   Dim sItem As String
   Picture1.Cls
  'loop trough the ListItems collection
   For hIndex = 0 To ListView1.ListItems.Count - 1
     'For each item in the collection, loop through the subitems.
     'Setting subitem to 0 retrieves the main listview item value.
      For hItem = 0 To 2
        'initialize the LV_ITEM type
         objItem.mask = LVIF_TEXT
         objItem.iSubItem = hItem
         objItem.pszText = Space$(32)
         objItem.cchTextMax = Len(objItem.pszText)
        'get the value of the item corresponding
        'to the index and item numbers
         r = SendMessageAny(ListView1.hWnd, LVM_GETITEMTEXT, hIndex,
objItem)
        'and trim the item
         sItem = Left$(objItem.pszText, r)
        'print the item with a tab (Note the terminating comma!!)
         Picture1.Print sItem,
      Next hItem
     'move to the next line
      Picture1.Print
   Next hIndex
End Sub
Private Sub cmdEnd_Click()
   Unload Me
   End
End Sub
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As
ComctlLib.ColumnHeader)
  'toggle the sortorder for use in the CompareXX routines
   sOrder = Not sOrder
   ListView1.SortKey = ColumnHeader.Index - 1
   Select Case ColumnHeader.Index - 1
      Case 0:
              'Use default sorting to sort the items in the list
               ListView1.SortKey = 0
               ListView1.SortOrder = Abs(sOrder) 'Abs(Not
ListView1.SortOrder = 1)
               ListView1.Sorted = True
      Case 1:
              'Use sort routine to sort by date
               ListView1.Sorted = False
               SendMessageLong ListView1.hWnd, _
                               LVM_SORTITEMS, _
                               ListView1.hWnd, _
                               AddressOf CompareDates
      Case 2:
              'Use sort routine to sort by value
               ListView1.Sorted = False
               SendMessageLong ListView1.hWnd, _
                               LVM_SORTITEMS, _
                               ListView1.hWnd, _
                               AddressOf CompareValues
   End Select
End Sub
'--end block--'
 Comments
            Save & run the project. The listview will be populated with the
data
            shown in the illustration.
            Clicking the "Name" column header will invoke the control's
default
            string-sorting routine. Clicking either of the "Date" or "Value"
            headers will invoke the callback routines. Repeated clicking
will
            toggle the sort between ascending and descending.
            The purpose of the two command buttons to print out the display
is
            to demonstrate that while the sort is maintained by the listview
via
            the default sort mechanism, the listitems collection is also
sorted
            in unison. However sorting using the callbacks throws out its
            visual-collection unison - try sorting on the Value field, and
then
            click the "via Collection" button. The printout is the same as
it
            was after Name column was last sorted. But pressing the "via
API"
            button uses the Listview LV_ITEM iItem member (try adding the
line:
            Picture1.Print objItem.iItem, (including the trailing comma))
just
            before the last Picture1.Print statement in the cmdDisplayAPI
            routine). Regardless of the order that the listview is tracking
            internally, the .iItem will always point to the correct item.
Stefano Zamboni
 (W95 4.00.950a & IE4 & VB5SP3 ed ora anche Delphi2)
----------------------------------------------------------------------------
----------------
sz...@XXXtin.it
zam...@XXXfreemail.it
Togliere "XXX" per rispondere
Il sito comune di programmatori VB
http://members.xoom.com/it_lang_vb
----------------------------------------------------------------------------
----------------