Fetstil Fetstil Kursiv Understrykning linje färgläggning tabellverk Punktlista Nummerlista Vänster Centrerat högerställt Utfyllt Länk Bild htmlmode
  • Forum & Blog
    • Forum - översikt
      • .Net
        • asp.net generellt
        • c#
        • vb.net
        • f#
        • silverlight
        • microsoft surface
        • visual studio .net
      • databaser
        • sql-server
        • databaser
        • access
        • mysql
      • mjukvara klient
        • datorer och komponenter
        • nätverk, lan/wan
        • operativsystem
        • programvaror
        • säkerhet, inställningar
        • windows server
        • allmänt
        • crystal reports
        • exchange/outlook
        • microsoft office
      • mjukvara server
        • active directory
        • biztalk
        • exchange
        • linux
        • sharepoint
        • webbservers
        • sql server
      • appar (win/mobil)
      • programspråk
        • c++
        • delphi
        • java
        • quick basic
        • visual basic
      • scripting
        • asp 3.0
        • flash actionscript
        • html css
        • javascript
        • php
        • regular expresssion
        • xml
      • spel och grafik
        • DirectX
        • Spel och grafik
      • ledning
        • Arkitektur
        • Systemutveckling
        • krav och test
        • projektledning
        • ledningsfrågor
      • vb-sektioner
        • activeX
        • windows api
        • elektronik
        • internet
        • komponenter
        • nätverk
        • operativsystem
      • övriga forum
        • arbete karriär
        • erbjuda uppdrag och tjänster
        • juridiska frågor
        • köp och sälj
        • matematik och fysik
        • intern information
        • skrivklåda
        • webb-operatörer
    • Posta inlägg i forumet
    • Chatta med andra
  • Konto
    • Medlemssida
    • Byta lösenord
    • Bli bonsumedlem
    • iMail
  • Material
    • Tips & tricks
    • Artiklar
    • Programarkiv
  • JOBB
  • Student
    • Studentlicenser
  • KONTAKT
    • Om pellesoft
    • Grundare
    • Kontakta oss
    • Annonsering
    • Partners
    • Felanmälan
  • Logga in

Hem / Forum översikt / inlägg

Posta nytt inlägg


Sortera i listview kontrollen...

Postades av 2001-11-17 18:25:00 - Ludwig Noid, i forum visual basic - allmänt, Tråden har 3 Kommentarer och lästs av 971 personer

Hej jag undrar hur man sorterar data i ListView funktionen på samma sätt som man gör i utforskaren. Alltså genom att clicka på de gråa rubrikerna.
Tack på förhand


Svara

Sv: Sortera i listview kontrollen...

Postades av 2001-11-17 23:06:00 - Claes Persson

Följande hittade jag i hjälptexten för SortKey...

<code>
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ColumnHeader)
' When a ColumnHeader object is clicked, the ListView control is
' sorted by the subitems of that column.
' Set the SortKey to the Index of the ColumnHeader - 1
ListView1.SortKey = ColumnHeader.Index - 1
' Set Sorted to True to sort the list.
ListView1.Sorted = True
End Sub
</code>

Det ger nästan samma effekt, i Utforskaren kan du ändra sorteringen från stigande till fallande och tillbaka igen genom att klicka flera gånger på rubriken. DET fungerar inte i exemplet ovan, men det går väl också att fixa antar jag...


Svara

Sv: Sortera i listview kontrollen...

Postades av 2001-11-17 23:31:00 - Johan Nykvist

Här kommer en kodsnutt jag brukar använda

Modul:
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

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 POINTAPI
x As Long
y As Long
End Type

Public Type LV_FINDINFO
flags As Long
psz As String
lParam As Long
pt As POINTAPI
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 As Long = &H1
Public Const LVIF_TEXT As Long = &H1

Public Const LVM_FIRST As Long = &H1000
Public Const LVM_FINDITEM As Long = (LVM_FIRST + 13)
Public Const LVM_GETITEMTEXT As Long = (LVM_FIRST + 45)
Public Const LVM_SORTITEMS As Long = (LVM_FIRST + 48)

'API declarations
Public Declare Function SendMessage 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(hwnd, lParam1)
dDate2 = ListView_GetItemDate(hwnd, lParam2)

'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(hwnd As Long, lParam As Long) As Date

Dim hIndex As Long
Dim R As Long

'Convert the input parameter to an index in the list view
objFind.flags = LVFI_PARAM
objFind.lParam = lParam
hIndex = SendMessage(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 = 4
objItem.pszText = Space$(32)
objItem.cchTextMax = Len(objItem.pszText)

'get the string at subitem 1
'and convert it into a date and exit
R = SendMessage(hwnd, LVM_GETITEMTEXT, hIndex, objItem)
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 hIndex As Long
Dim R As Long

'Convert the input parameter to an index in the list view
objFind.flags = LVFI_PARAM
objFind.lParam = lParam
hIndex = SendMessage(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 = 3
objItem.pszText = Space$(32)
objItem.cchTextMax = Len(objItem.pszText)

'get the string at subitem 2
'and convert it into a long
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

'A procedure that receives and returns
'the value of the AddressOf operator.
'This workaround is needed as you can't assign
'AddressOf directly to an API when you are also
'passing the value ByVal in the statement
'(as is being done with SendMessage)

FARPROC = pfn

End Function










Form:
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ComctlLib.ColumnHeader)
'toggle the sort order for use in the CompareXX routines
sOrder = Not sOrder

ListView1.SortKey = ColumnHeader.Index - 1

Select Case ColumnHeader.Index - 1
Case 0, 1, 2, 6, 7, 8 'Sorterar kolumn 0,1,2,6,7,8 efter alfabetet
'Use default sorting to sort the items in the list
ListView1.SortKey = ColumnHeader.Index - 1
ListView1.SortOrder = Abs(sOrder) '=Abs(Not ListView1.SortOrder = 1)
ListView1.Sorted = True

Case 5: 'sorterar kolumn 5 efter datum
'Use sort routine to sort by date
ListView1.Sorted = False
objItem.iSubItem = ColumnHeader.Index - 1
SendMessage ListView1.hwnd, _
LVM_SORTITEMS, _
ListView1.hwnd, _
ByVal FARPROC(AddressOf CompareDates)

Case 3, 4, 9 'sorterar kolumn 3,4,9 efter sifferordning
'Use sort routine to sort by value
ListView1.Sorted = False
objItem.iSubItem = ColumnHeader.Index - 1
SendMessage ListView1.hwnd, _
LVM_SORTITEMS, _
ListView1.hwnd, _
ByVal FARPROC(AddressOf CompareValues)

End Select


End Sub


Lycka till

Johan


Svara

Sv: Sortera i listview kontrollen...

Postades av 2001-11-18 14:34:00 - Andreas Hillqvist

Lite overkill tycker jag.


Svara

Nyligen

  • 14:24 CBD regelbundet?
  • 14:23 CBD regelbundet?
  • 14:22 Har du märkt några verkliga fördel
  • 09:09 Vill du köpa medicinska tester?
  • 12:47 Vem beviljar assistansen – kommune
  • 14:17 Någon med erfarenhet av hemstädnin
  • 14:14 Bör man använda sig av en båtförme
  • 14:12 Finns det någon intressant hundblo

Sidor

  • Hem
  • Bli bonusmedlem
  • Läs artiklar
  • Chatta med andra
  • Sök och erbjud jobb
  • Kontakta oss
  • Studentlicenser
  • Skriv en artikel

Statistik

Antal besökare:
Antal medlemmar:
Antal inlägg:
Online:
På chatten:
4 569 627
27 953
271 710
743
0

Kontakta oss

Frågor runt konsultation, rådgivning, uppdrag, rekrytering, annonsering och övriga ärenden. Ring: 0730-88 22 24 | pelle@pellesoft.se

© 1986-2013 PelleSoft AB. Last Build 4.1.7169.18070 (2019-08-18 10:02:21) 4.0.30319.42000
  • Om
  • Kontakta
  • Regler
  • Cookies