Jump to content

Listbox - Drag / Drop Items


Recommended Posts

Hi all,

Has anyone got any code to move items around a listbox, (rearrange them etc).

Tried searching the forum, but obviously using the wrong search terms.

Thanks in advance

----[ SandyD ]---
Link to comment
Share on other sites

I don't think you can do that in a Listbox. You MAY be able to do it in a ListView though.

Mark.

Yes, here is a way you could do it with a listview. It's not very elgant but as long as noone's watching-

(Adapted by adding drag and drop to code by Gyzmok post earlier)

;Autoit beta version v3.2.3.12(beta)
#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <Misc.au3>
#include-once

Global Const $WM_NOTIFY       = 0x004E

Global Const $NM_FIRST = 0
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)

$GUI =  GuiCreate("Listview",270,400,-1,-1)
$mylist =   GUICtrlCreateListView("Val1|Val2|Val3", 45, 50, 200, 310, _
        BitOR($LVS_SHOWSELALWAYS,$LVS_REPORT), _
        BitOR($LVS_EX_FULLROWSELECT,$LVS_EX_CHECKBOXES, $LVS_EX_FLATSB ))
$label1 = GUICtrlCreateLabel("",70,10,50,20)
$label2 = GUICtrlCreateLabel("",110,10,50,20)
$label3 = GUICtrlCreateLabel("",150,10,50,20)
$pend = 0
for $i = 1 to 50
    _GUICtrlListViewInsertItem($mylist,$i-1,$i&"|"&$i&"|"&$i)
next
Sleep(3000)
GUISetState(@SW_SHOW,$gui)
;GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
$dragging = False
$dragitem = 0

$pend = TimerInit()

While 1
    If TimerDiff($pend) > 300  Then
        _getinfo()
        $pend = TimerInit()
    EndIf
    
    $msg = GuiGetMsg(1)
    Select
        Case $msg[0] = $GUI_EVENT_CLOSE
            Exit
        Case $msg[0] = $GUI_EVENT_PRIMARYDOWN
            ;MsgBox(0,'ctrl id','$myList handle = ' & GUICtrlGetHandle($mylist) & @CRLF & WinGetHandle($GUI) & @CRLF & $msg[1] & ',  ' & $msg[2])
            If Inview($msg[3],$msg[4]) Then
                $dragging = True
                $dragitem = _GUICtrlListViewGetCurSel($mylist)
            Else
                $dragging = False
            EndIf
            
        Case $msg[0] = $GUI_EVENT_PRIMARYUP
            If inview($msg[3],$msg[4]) Then
                MouseClick("left")
                $newitem = _GUICtrlListViewGetCurSel($mylist)
                
                If $dragitem <> $newitem Then
                    If $dragitem > $newitem Then
                        $offset = 1
                    Else
                        $offset = 0
                    EndIf
                    $dragtext = _GUICtrlListViewGetItemText($mylist,$dragitem)
                    _GUICtrlListViewInsertItem($mylist,$newitem,$dragtext)
                    _GUICtrlListViewDeleteItem($mylist,$dragitem + $offset)
                    
                EndIf
                
                $dragging = False
                
            EndIf
            ; $newite
        Case Else
    EndSelect
    If _IsPressed("26")and BitAnd(WinGetState("Listview", ""), 8) Then; UP key
        _getinfo()
        ;$pend = 1
    EndIf
    If _IsPressed("28")and BitAnd(WinGetState("Listview", ""), 8) Then; DOWN key
        _getinfo()
        ;$oend = 1
    EndIf
WEnd

Func InView($x,$y)
    ;Return True
    If $x > 45 And $x  < 200 And $y > 50 And $y < 310 Then
        Return True
    Else
        ;MsgBox(0,'mouse xy',$mp[0] & ', ' & $mp[1])
        Return False
    Endif
    
EndFunc


Func _getinfo()
    $data = _GUICtrlListViewGetItemTextArray($mylist,-1)
    If Not IsArray($data) Then Return
    _setdata($label1,$data[1])
    _setdata($label2,$data[2])
    _setdata($label3,$data[3])
EndFunc ;==>_getinfo()
;
Func _setdata($label,$field)
    GUICtrlSetData($label,$field)
EndFunc ;==>_setdata($field,$label)

Edit:Same approach could be used with a list box

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...