Sign in to follow this  
Followers 0
ReFran

LV move item up and down

3 posts in this topic

I didn't use it for a longer time. It worked up to ca. 3.1.100. Also an old example from Gary doesn't work as awaited. http://www.autoitscript.com/forum/index.ph...inhard&st=0

if I move a file named 5 up, it works 2 times correct (5->4,del6 and 4->3,del5), but then it moves correct from 3->2, the msgbox show correct del4, but item 6 - the last item - will be deleted.

Perhaps someone can correct the script or give an example of moving items in a LV up and down.

Thanks in advance, Reinhard

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LV_FolderFiles_SortSelMoveDel
;; Get Files from Folder and Sort
;; Sort LV-Items by clicking on header and move up/down intems by button
;; LV-Files/Folder/Sorting and .. and ... and examples from Gary
;; Wingdings Buttons from BigDod
;; Puzzled together from ReFran
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


#include <GuiConstants.au3>
#include <GuiListView.au3>

;opt('MustDeclareVars', 1)
Dim $listview, $font, $Btn_MoveUp, $Btn_Movedn, $Btn_Exit, $msg, $Status, $Btn_Insert, $ret, $Input_Index, $pos
GUICreate("ListView Sort", 392, 322)

$listview = GUICtrlCreateListView("File|Size|Type|Date Created|Date Modified", 40, 30, 310, 149, -1, BitOR($LVS_EX_REGIONAL, $LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
_GUICtrlListViewSetColumnWidth ($listview, 0, 75)
_GUICtrlListViewSetColumnWidth ($listview, 1, 75)
_GUICtrlListViewSetColumnWidth ($listview, 2, 75)
_GUICtrlListViewSetColumnWidth ($listview, 3, 75)
_GUICtrlListViewSetColumnWidth ($listview, 4, 75)

$Menlv_context = GUICtrlCreateContextMenu($listview)
    $Menlv_DelFile = GUICtrlCreateMenuitem("Delete from List",$MenLV_context)

$font="Wingdings"
$Btn_MoveUp = GUICtrlCreateButton("é", 150, 190, 30, 30)
    GUICtrlSetFont (-1,18, 400, 1, $font)
$Btn_MoveDn = GUICtrlCreateButton("ê", 200, 190, 30, 30)
    GUICtrlSetFont (-1,18, 400, 1, $font)

$Btn_Exit = GUICtrlCreateButton("Exit", 300, 260, 70, 30)
$Status = GUICtrlCreateLabel("", 0, 302, 392, 20, BitOR($SS_SUNKEN, $SS_CENTER))
GUISetState()
_PopulateListView($listview)
Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount ($listview) ]

While 1
   $msg = GUIGetMsg()
   Select
    Case $msg = $GUI_EVENT_CLOSE Or $msg = $Btn_Exit
         ExitLoop
    Case $msg = $listview
    ; sort the list by the column header clicked on
         _GUICtrlListViewSort ($listview, $B_DESCENDING, GUICtrlGetState($listview))
    Case $msg = $GUI_EVENT_PRIMARYDOWN
        $pos = GUIGetCursorInfo()
        If ($pos[4] == $listview) Then
            GUICtrlSetData($Status, "Current Index Selected: " & _GUICtrlListViewGetCurSel ($listview))
        EndIf
    Case $msg = $Btn_MoveUP
        if _GUICtrlListViewGetSelectedCount ($listview) = 0 then 
              msgBox(0,"","Nothing selected")
              Else
              _GUICtrlListViewMoveItem($listview, _GUICtrlListViewGetCurSel($listview), _GUICtrlListViewGetCurSel($listview)-1)
        EndIf
    Case $msg = $Btn_MoveDN
        if _GUICtrlListViewGetSelectedCount ($listview) = 0 then 
              msgBox(0,"","Nothing selected")
              Else
              _GUICtrlListViewMoveItem($listview, _GUICtrlListViewGetCurSel($listview), _GUICtrlListViewGetCurSel($listview)+1)
          EndIf
    Case $msg = $MenLv_DelFile
          if _GUICtrlListViewGetSelectedCount ($listview) = 0 then 
              msgBox(0,"","Nothing selected")
              Else
             _GUICtrlListViewDeleteItemsSelected ($listview)
             EndIf
   EndSelect
WEnd
Exit

Func _PopulateListView(ByRef $listview)
   Local $startdir, $var, $search, $file, $item, $attrib, $time
   $startdir = @MyDocumentsDir
   
   $var = FileSelectFolder("Choose a folder.", $startdir)
   If $var <> "" Then
      FileChangeDir($var)
; Shows the filenames of all files in the current directory.
      $search = FileFindFirstFile("*.*")
      
; Check if the search was successful
      If $search = -1 Then
         Return
      EndIf
      
      While 1
         $file = FileFindNextFile($search)
         If @error Then ExitLoop
         $item = $file & "|"
         $attrib = FileGetAttrib($file)
         If Not @error Then
            If StringInStr($attrib, "D") Then
               $item = $file & "||Directory|"
            Else
               $item = $file & "|" & FileGetSize($file) & " bytes" & "|File|"
            EndIf
         EndIf
         $time = FileGetTime($file, 1)
         
         If Not @error Then
            $item = $item & $time[0] & "/" & $time[1] & "/" & $time[2] & " "
            If Int($time[3]) > 12 Then
               $item = $item & $time[3] - 12 & ":" & $time[4] & " PM|"
            Else
               $item = $item & $time[3] & ":" & $time[4] & " AM|"
            EndIf
         EndIf
         $time = FileGetTime($file)
         If Not @error Then
            $item = $item & $time[0] & "/" & $time[1] & "/" & $time[2] & " "
            If Int($time[3]) > 12 Then
               $item = $item & $time[3] - 12 & ":" & $time[4] & " PM"
            Else
               $item = $item & $time[3] & ":" & $time[4] & " AM"
            EndIf
         EndIf
         GUICtrlCreateListViewItem($item, $listview)
      WEnd
      
; Close the search handle
      FileClose($search)
      
   EndIf
EndFunc;==>_PopulateListView

;;;;;;;;;;;;;LV-Move items For MoveUp and DN buttons;;;;;;;;;;;;;;;;;;;;;;;;
Func _GUICtrlListViewMoveItem(ByRef $h_listview, $i_from = 0, $i_to = 0)
   Local $Max = _GUICtrlListViewGetItemCount ($h_listview) 
   Local $ret, $m_item
   $i_from = Int($i_from)
   $i_to = Int($i_to)
;msgBox(0,"",$max &" " &$i_from &"->" &$i_to)
   If $i_from >= 0 And $i_from <= $Max And $i_to >= 0 And $i_to <= $Max And $i_from <> $i_to Then
      $m_item = _GUICtrlListViewGetItemText ($h_listview, $i_from)
;msgBox(0,"","Mitem " &$m_item)
      If $m_item <> $LV_ERR Then
         $ret = _GUICtrlListViewInsertItem ($h_listview, $i_to, $m_item)
         If $i_from > $i_to And $ret <> $LV_ERR Then
            $xup = _GUICtrlListViewDeleteItem ($h_listview, $i_from +1)
            _GUICtrlListViewSetItemSelState($h_listview, $i_to, 1)
         Else
            $xdown = _GUICtrlListViewDeleteItem ($h_listview, $i_from)
            _GUICtrlListViewSetItemSelState($h_listview, $i_to-1, 1)
         EndIf
      EndIf
   EndIf
   Return $ret
EndFunc;==>_GUICtrlListViewMoveItem

Share this post


Link to post
Share on other sites



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LV_FolderFiles_SortSelMoveDel
;; Get Files from Folder and Sort
;; Sort LV-Items by clicking on header and move up/down intems by button
;; LV-Files/Folder/Sorting and .. and ... and examples from Gary
;; Wingdings Buttons from BigDod
;; Puzzled together from ReFran
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


#include <GuiConstants.au3>
#include <GuiListView.au3>

;opt('MustDeclareVars', 1)
Dim $listview, $font, $Btn_MoveUp, $Btn_Movedn, $Btn_Exit, $msg, $Status, $Btn_Insert, $ret, $Input_Index, $pos
GUICreate("ListView Sort", 392, 322)

$listview = GUICtrlCreateListView("File|Size|Type|Date Created|Date Modified", 40, 30, 310, 149, -1, BitOR($LVS_EX_REGIONAL, $LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES))
_GUICtrlListViewSetColumnWidth($listview, 0, 75)
_GUICtrlListViewSetColumnWidth($listview, 1, 75)
_GUICtrlListViewSetColumnWidth($listview, 2, 75)
_GUICtrlListViewSetColumnWidth($listview, 3, 75)
_GUICtrlListViewSetColumnWidth($listview, 4, 75)

$Menlv_context = GUICtrlCreateContextMenu($listview)
$Menlv_DelFile = GUICtrlCreateMenuitem("Delete from List", $Menlv_context)

$font = "Wingdings"
$Btn_MoveUp = GUICtrlCreateButton("é", 150, 190, 30, 30)
GUICtrlSetFont(-1, 18, 400, 1, $font)
$Btn_Movedn = GUICtrlCreateButton("ê", 200, 190, 30, 30)
GUICtrlSetFont(-1, 18, 400, 1, $font)

$Btn_Exit = GUICtrlCreateButton("Exit", 300, 260, 70, 30)
$Status = GUICtrlCreateLabel("", 0, 302, 392, 20, BitOR($SS_SUNKEN, $SS_CENTER))
GUISetState()
_PopulateListView($listview)
Dim $B_DESCENDING[_GUICtrlListViewGetSubItemsCount($listview) ]

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $Btn_Exit
            ExitLoop
        Case $msg = $listview
            ; sort the list by the column header clicked on
            _GUICtrlListViewSort($listview, $B_DESCENDING, GUICtrlGetState($listview))
        Case $msg = $GUI_EVENT_PRIMARYDOWN
            $pos = GUIGetCursorInfo()
            If ($pos[4] == $listview) Then
                GUICtrlSetData($Status, "Current Index Selected: " & _GUICtrlListViewGetCurSel($listview))
            EndIf
        Case $msg = $Btn_MoveUp
            If _GUICtrlListViewGetSelectedCount($listview) = 0 Then
                MsgBox(0, "", "Nothing selected")
            Else
                _GUICtrlListViewMoveItem($listview, _GUICtrlListViewGetCurSel($listview), _GUICtrlListViewGetCurSel($listview) - 1)
            EndIf
        Case $msg = $Btn_Movedn
            If _GUICtrlListViewGetSelectedCount($listview) = 0 Then
                MsgBox(0, "", "Nothing selected")
            Else
                _GUICtrlListViewMoveItem($listview, _GUICtrlListViewGetCurSel($listview), _GUICtrlListViewGetCurSel($listview) + 1)
            EndIf
        Case $msg = $Menlv_DelFile
            If _GUICtrlListViewGetSelectedCount($listview) = 0 Then
                MsgBox(0, "", "Nothing selected")
            Else
                _GUICtrlListViewDeleteItemsSelected($listview)
            EndIf
    EndSelect
WEnd
Exit

Func _PopulateListView(ByRef $listview)
    Local $startdir, $var, $search, $file, $item, $attrib, $time
    $startdir = @MyDocumentsDir

    $var = FileSelectFolder("Choose a folder.", $startdir)
    If $var <> "" Then
        FileChangeDir($var)
        ; Shows the filenames of all files in the current directory.
        $search = FileFindFirstFile("*.*")

        ; Check if the search was successful
        If $search = -1 Then
            Return
        EndIf

        While 1
            $file = FileFindNextFile($search)
            If @error Then ExitLoop
            $item = $file & "|"
            $attrib = FileGetAttrib($file)
            If Not @error Then
                If StringInStr($attrib, "D") Then
                    $item = $file & "||Directory|"
                Else
                    $item = $file & "|" & FileGetSize($file) & " bytes" & "|File|"
                EndIf
            EndIf
            $time = FileGetTime($file, 1)

            If Not @error Then
                $item = $item & $time[0] & "/" & $time[1] & "/" & $time[2] & " "
                If Int($time[3]) > 12 Then
                    $item = $item & $time[3] - 12 & ":" & $time[4] & " PM|"
                Else
                    $item = $item & $time[3] & ":" & $time[4] & " AM|"
                EndIf
            EndIf
            $time = FileGetTime($file)
            If Not @error Then
                $item = $item & $time[0] & "/" & $time[1] & "/" & $time[2] & " "
                If Int($time[3]) > 12 Then
                    $item = $item & $time[3] - 12 & ":" & $time[4] & " PM"
                Else
                    $item = $item & $time[3] & ":" & $time[4] & " AM"
                EndIf
            EndIf
            _GUICtrlListViewInsertItem($listview, -1, $item)
;~          GUICtrlCreateListViewItem($item, $listview)
        WEnd

        ; Close the search handle
        FileClose($search)

    EndIf
EndFunc   ;==>_PopulateListView

;;;;;;;;;;;;;LV-Move items For MoveUp and DN buttons;;;;;;;;;;;;;;;;;;;;;;;;
Func _GUICtrlListViewMoveItem(ByRef $h_listview, $i_from = 0, $i_to = 0)
    Local $Max = _GUICtrlListViewGetItemCount($h_listview)
    Local $ret, $m_item
    $i_from = Int($i_from)
    $i_to = Int($i_to)
;~ msgBox(0,"",$max &" " &$i_from &"->" &$i_to)
    If $i_from >= 0 And $i_from <= $Max And $i_to >= 0 And $i_to <= $Max And $i_from <> $i_to Then
        $m_item = _GUICtrlListViewGetItemText($h_listview, $i_from)
;~ msgBox(0,"","Mitem " &$m_item)
        If $m_item <> $LV_ERR Then
            If $i_from > $i_to And $ret <> $LV_ERR Then
                $xup = _GUICtrlListViewDeleteItem($h_listview, $i_from)
                $ret = _GUICtrlListViewInsertItem($h_listview, $i_to, $m_item)
                _GUICtrlListViewSetItemSelState($h_listview, $i_to, 1, 1)
            Else
                $xdown = _GUICtrlListViewDeleteItem($h_listview, $i_from)
                $ret = _GUICtrlListViewInsertItem($h_listview, $i_to, $m_item)
                _GUICtrlListViewSetItemSelState($h_listview, $i_to, 1, 1)
            EndIf
        EndIf
    EndIf
    Return $ret
EndFunc   ;==>_GUICtrlListViewMoveItem


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Hi Gary,

thank you for the correction. It works now perfect. After some bigger projects on work and some problems with my computers I hope I can now go on with scripting.

Best regards, Reinhard

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