Jump to content

Listview drag and drop


Recommended Posts

The drag and drop functionality of this script is working correctly as long as I just drag the item "Down", however if I drag an item from the bottom of the list "Up" then the item is duplicated and not moved. Any idea why this is happening, or any other suggestions would be greatly apreciated.

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("Test Listview ", 569, 391, 872, 128)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
Global $listview = GUICtrlCreateListView("Order|    File  ", 41, 50, 464, 216, _
    BitOR($LVS_SHOWSELALWAYS,$LVS_REPORT), _
    BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_FLATSB ))
GUICtrlSendMsg($listview, 0x101E, 0, 50)
GUICtrlSendMsg($listview, 0x101E, 1, 390)
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_ClickDn")
GUISetOnEvent($GUI_EVENT_PRIMARYUP, "_ClickUp")

$mnu = GUICtrlCreateContextMenu($Listview)
$mnu_item = GUICtrlCreateMenuitem("Delete", $mnu)
GUISetOnEvent($mnu_item, "_MenuItem")
GUISetState(@SW_SHOW,$gui)

Global $dragging = False
Global $dragitem = 0

_IniKey_to_Array("Temp.ini","Drivers32","total") 
Sleep(3000)

While 1
    Sleep(50) ;Dont cook my CPU
WEnd

Func _IniKey_to_Array($File,$Section,$Key,$Verbose = 0) ;First key entry must be total number of keys in section
    $Total = iniread($File, $Section, $Key, "Default")
    If $Verbose = 1 Then MsgBox(0,'Verify','Verify Total is being read from file, Total= '&$Total,0)
    Global $Array = _ArrayCreate("")
    _ArrayInsert($Array,0, $Total)
    For $i = 1 to $Total
        $e = iniread($File, $Section, $i, "Default")
        _ArrayInsert($Array,$i, $e)
    Next
    _ArrayDelete( $Array,$Total +1)
    If $Verbose = 1 Then _ArrayDisplay( $Array, "Array" ) 

    For $i = 1 to $Total
        GUICtrlCreateListViewItem($i&"|"&$Array[$i], $listview)
    Next
    
EndFunc

Func _Exit()
    Exit
EndFunc

Func _ClickDn()
    If _Inview() Then
        $dragging = True
        $dragitem = _GUICtrlListViewGetCurSel($listview)
    Else
        $dragging = False
    EndIf   
EndFunc 

Func _ClickUp()
    If _inview() Then
        MouseClick("left")
        $newitem = _GUICtrlListViewGetCurSel($listview)
        If $dragitem <> $newitem Then
            $dragtext = _GUICtrlListViewGetItemText($listview,$dragitem)
            _GUICtrlListViewInsertItem($listview,$newitem,$dragtext)
            _GUICtrlListViewDeleteItem($listview,$dragitem)
        EndIf
        $dragging = False
    EndIf
EndFunc 

Func _MenuItem()
            MsgBox(0,'','delete Item ?',0)
    $Selected =  _GUICtrlListViewGetCurSel($listview)
    _GUICtrlListViewDeleteItem($listview,$Selected) 
EndFunc 

Func _InView(); Narrow the  click down to just the Listview
    $MLoc = Mousegetpos()
    $size = WinGetPos($GUI) ;GUI window stats (x,y,width,height) $size[0], $size[1], $size[0], $size[3]
    ;Return True
    If ($size[0]+45) < $MLoc[0] And ($size[0]+470) > $MLoc[0] And ($size[1]+50) < $MLoc[1] And ($size[1]+310) > $MLoc[1] Then
        Return True
    Else
        Return False
    Endif
EndFunc

The file the script refrences, a simple ini named Temp.ini

[drivers32]
total=13
1=a
2=b
3=c
4=d
5=e
6=f
7=g
8=h
9=i
10=j
11=k
12=l
13=m

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Link to comment
Share on other sites

The problem is that the dragitem is still selected or focused.

Replace your Func _ClickUp() with this one.

Func _ClickUp()
    If _inview() Then
        MouseClick("left")
        $newitem = _GUICtrlListViewGetCurSel($listview)
          $dragtext = _GUICtrlListViewGetItemTexT($listview,$dragitem)
        Select
            Case $dragitem < $newitem
                _GUICtrlListViewInsertItem($listview,$newitem,$dragtext)
            _GUICtrlListViewDeleteItem($listview,$dragitem)
            Case $dragitem > $newitem
                _GUICtrlListViewSetItemSelState($listview,$newitem,0,0) 
                _GUICtrlListViewInsertItem($listview,$newitem,$dragtext)
                _GUICtrlListViewSetItemSelState($listview,$newItem,1,1) 
                _GUICtrlListViewDeleteItem($listview,$dragitem+1)
        EndSelect
        $dragging = False
    EndIf
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...