Re: Sort Listview after drag and drop not working correctly
"valery_vi" <[email protected]> Wed, 18 Jul 2007 10:41:59 -0000
| Newsgroups | gmane.comp.windows.autoit.user |
|---|---|
| Message-ID | <[email protected]> |
momar33 from AutoItScript forum wrote:
http://www.autoitscript.com/forum/index.php?showtopic=49677
"
If you run the code below you will see it has two listview items. Now
if you try to sort them they work fine and when you drag a line from
one to the other, it works fine. The problem is when you drag on or
more to the other listview and then try to sort. I have found that
when it trys to get the item in GetSubItemText, all it gets is 0.
Any ideas for this.
"
Below is valid code of this script:
; *******************************************************
; Example 1 - sorting 3 column's different
; *******************************************************
#include <GUIConstants.au3>
#Include <GuiListView.au3>
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
;Global Const $LVFI_PARAM = 0x0001
;Global Const $LVIF_TEXT = 0x0001
;Global Const $LVM_FIRST = 0x1000
Global Const $LVM_GETITEM = $LVM_FIRST + 5
;Global Const $LVM_FINDITEM = $LVM_FIRST + 13
;Global Const $LVM_SETSELECTEDCOLUMN = $LVM_FIRST + 140
Dim $nCurCol = -1
Dim $nSortDir = 1
Dim $bSet = 0
Dim $nCol = -1
$hGUI = GUICreate("Test", 300, 400)
GUISetOnEvent($GUI_EVENT_DROPPED, "Drop")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$lv = GUICtrlCreateListView("Column1|Col2|Col3", 10, 10, 280, 180,
$LVS_SHOWSELALWAYS)
GUICtrlRegisterListViewSort(-1, "LVSort") ; Register the function
"SortLV" for the sorting callback
GUICtrlSetState(-1,$GUI_DROPACCEPTED)
GUICtrlSetOnEvent(-1, "LV")
GUICtrlCreateListViewItem("Abi-Dalzim's Horrid Wilting|8|Illusion", $lv)
GUICtrlCreateListViewItem("Combust (Red Wizard)|2|Alteration", $lv)
GUICtrlCreateListViewItem("Destroy Undead|4|Necromancy", $lv)
GUICtrlCreateListViewItem("Egg|5|Divination", $lv)
GUICtrlCreateListViewItem("Fireball|3|Evocation", $lv)
$lv = GUICtrlCreateListView("Column1|Col2|Col3", 10, 210, 280, 180,
$LVS_SHOWSELALWAYS)
GUICtrlRegisterListViewSort(-1, "LVSort") ; Register the function
"SortLV" for the sorting callback
GUICtrlSetState(-1,$GUI_DROPACCEPTED)
GUICtrlSetOnEvent(-1, "LV")
GUICtrlCreateListViewItem("Abi-Dalzim's Horrid Wilting|8|Illusion", $lv)
GUICtrlCreateListViewItem("Combust (Red Wizard)|2|Alteration", $lv)
GUICtrlCreateListViewItem("Suck Life|6|Necromancy", $lv)
GUICtrlCreateListViewItem("Egg|5|Divination", $lv)
GUICtrlCreateListViewItem("Bolt|3|Evocation", $lv)
GUISetState()
While 1
Sleep(10)
WEnd
Exit
Func _Exit()
Exit
EndFunc
Func Drop()
Local $aIndices =_GUICtrlListViewGetSelectedIndices(@GUI_DRAGID, 1)
local $ret[3]
ConsoleWrite("Drag ID: " & @GUI_DRAGID & @CR)
;ConsoleWrite(_GUICtrlListViewGetSelectedIndices(@GUI_DRAGID,1) & @CR)
ConsoleWrite("Drop ID: " & @GUI_DROPID & @CR)
For $i = 1 to $aIndices[0]
GUICtrlCreateListViewItem(_GUICtrlListViewGetItemText(@GUI_DRAGID,$aIndices[$i]),
@GUI_DROPID)
;ConsoleWrite($ret[$i-1] & @CR)
;_GUICtrlListViewInsertItem(@GUI_DROPID,-1,)
Next
EndFunc
Func LV()
$bSet = 0
$nCurCol = $nCol
GUICtrlSendMsg($lv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($lv), 0)
DllCall("user32.dll", "int", "InvalidateRect", "hwnd",
GUICtrlGetHandle($lv), "int", 0, "int", 1)
EndFunc
; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
; Switch the sorting direction
If $nColumn = $nCurCol Then
If Not $bSet Then
$nSortDir = $nSortDir * -1
$bSet = 1
EndIf
Else
$nSortDir = 1
EndIf
$nCol = $nColumn
$val1 = GetSubItemText($lv, $nItem1, $nColumn)
$val2 = GetSubItemText($lv, $nItem2, $nColumn)
$nResult = 0 ; No change of item1 and item2 positions
If $val1 < $val2 Then
$nResult = -1 ; Put item2 before item1
ElseIf $val1 > $val2 Then
$nResult = 1 ; Put item2 behind item1
EndIf
$nResult = $nResult * $nSortDir
Return $nResult
EndFunc
Func GetSubItemText($nCtrlID, $nItem1, $nColumn)
$subItemArray = StringSplit(GUICtrlRead($nItem1), "|")
ConsoleWrite("-------" & GUICtrlRead($nItem1) & "------" & @CR)
Return $subItemArray[$nColumn+1]
EndFunc
Valery