Sign in to follow this  
Followers 0
momar33

Sort Listview after drag and drop not working correctly

3 posts in this topic

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.

Here is the code:

; *******************************************************
; 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]
        _GUICtrlListViewInsertItem(@GUI_DROPID,-1,_GUICtrlListViewGetItemText(@GUI_DRAGID,$aIndices[$i]))
        ;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

Share this post


Link to post
Share on other sites



Try this fixed script:

Valid script

Valery


The point of world view

Share this post


Link to post
Share on other sites

Thanks, that seems to work great.

For those who are wondering:

Replace:

_GUICtrlListViewInsertItem(@GUI_DROPID,-1,_GUICtrlListViewGetItemText(@GUI_DRAGID, $aIndices[$i]))oÝ÷ Ùh­«­¢+ÙU%
Ñɱ
ÉÑ1¥ÍÑY¥Ý%Ñ´¡}U%
Ñɱ1¥ÍÑY¥ÝÑ%ѵQáСU%}I%°ÀÌØí%¹¥ÍlÀÌØí¥t¤±U%}I=A%¤

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0