VB6中ListBox、ListView数值排序问题
(2017-12-05 09:57:33)分类: VB |
VB6中ListBox、ListView数值排序问题
━━━━━━━━━━━━━━━━━━━━━━━━━
不知道各位有沒有發現,ListBox及 ComboBox 的 Sorted 屬性在遇到數字後有點怪怪的,是有排序沒錯,但是它似乎將數字當作文字排序了!所以排出來的效果和我們真正想要的並不完全相同。其實這情形不只發生在 ListBox 及 ComboBox 而已,檔案總管的檔案排也有同樣的情形,讓我們來看看:
ListBox及 ComboBox 的 Sorted 屬性設為 True
1 12 2
我們真正想要的結果
12 12
'以下這個模組就是用來排序數字用的,得到的結果,就如同上方右邊的結果!
Sub ReSort(L As Control)
Dim P%, PP%, C%, Pre$, S$, V
&
, NewPos%, CheckIt%
Dim TempL$, TempItemData
& , S1$
For P = 0 To L.ListCount - 1
S = L.List(P)
For C = 1 To Len (S)
V = Val(Mid$(S, C))
If V > 0 Then Exit For
Next
If V > 0 Then
If C > 1 Then Pre = Left$(S, C - 1)
NewPos = -
1
For PP = P + 1 To L.ListCount - 1
CheckIt = False
S1 = L.List(PP)
If Pre <> " " Then
If InStr (S1, Pre) = 1 Then CheckIt = True
Else
If Val (S1) > 0 Then CheckIt = True
End If
If CheckIt Then
If Val (Mid$(S1, C)) < V Then NewPos = PP
Else
Exit For
End If
Next
If NewPos > -
1 Then
TempL = L.List(P)
TempItemData = L.ItemData(P)
L.RemoveItem (P)
L.AddItem TempL, NewPos
L.ItemData(L.NewIndex) = TempItemData
P = P - 1
End If
End If
Next
EndSub
'實際使用時,需傳入控制項的名稱,例如:
PrivateSub Command1_Click()
Call ReSort(List1)
EndSub
━━━━━━━━━━━━━━━━━━━━━━━━━
找到一段api搞定的,有些聲明是多余的,自己整理吧..
三種排序方法已經封裝成函數
━━━━━━━━━━━━━━━━━━━━━━━━━
Public Enum LVStylesEx
CheckBoxes = LVS_EX_CHECKBOXES
FullRowSelect = LVS_EX_FULLROWSELECT
GridLines = LVS_EX_GRIDLINES
HeaderDragDrop = LVS_EX_HEADERDRAGDROP
SubItemImages = LVS_EX_SUBITEMIMAGES
TrackSelect = LVS_EX_TRACKSELECT
End Enum
'---Sorting Variables --- '
Public Enum LVItemTypes
lvDate = 0
lvNumber = 1
lvBinary = 2
lvAlphabetic = 3
End Enum
Public Enum LVSortTypes
lvAscending = 0
lvDescending = 1
End Enum
Public BuildLookup As Long
Public PerformSort As Long
'方法一:
'*********************************************************
' Knowledge Base-based Sorting Routines
'*********************************************************
Public Function LVSortK(lv As ListView, ByVal Index As Long, ByVal ItemType As LVItemTypes, ByVal SortOrder As LVSortTypes) As Boolean
Dim tmr As New CStopWatch
' turn off the default sorting of the control
With lv
.Sorted = False
. SortKey = Index
.SortOrder = SortOrder
End With
' store some values used during the sort
m_lvSortColumn = Index
m_lvSortType = ItemType
m_lvHWnd = lv.hWnd
BuildLookup = 0
' start sorting to type-specific callback routines
tmr.Reset
Select Case ItemType
Case lvDate
Call SendMessageLong(lv.hWnd, LVM_SORTITEMS, SortOrder, AddressOf
━━━━━━━━━━━━━━━━━━━━━━━━━
不知道各位有沒有發現,ListBox
ListBox
我們真正想要的結果
1
'以下這個模組就是用來排序數字用的,得到的結果,就如同上方右邊的結果!
Sub
End
Private
End
找到一段api搞定的,有些聲明是多余的,自己整理吧..
三種排序方法已經封裝成函數
━━━━━━━━━━━━━━━━━━━━━━━━━
Public
End
'---
End
Public
End
Public
Public
'方法一:
'
'
'