Jump to content

Moving items up and down in Listview


lyledg
 Share

Recommended Posts

Apologies for yet anorther posting, but I am trying to port my code to the newest version and having a lot of difficulties, so any help would be appreciated...

Thanks to this posting and florisch's code

#include <GUIConstants.au3>

#include <GuiListView.au3>

$Gui = GUICreate('Test Up&Down ListView', 300, 300)

$Listview = GUICtrlCreateListView("Column1|Column2", 20, 20)

For $i = 1 To 5

GUICtrlCreateListViewItem("Item "&$i&"|Item "&$i&" subtext", $Listview)

Next

$Up = GUICtrlCreateButton("Up", 20, 180)

$Down = GUICtrlCreateButton("Down", 90, 180)

GUISetState()

While 1

$msg = GUIGetMsg()

Switch $msg

Case -3

ExitLoop

Case $Up

_updown(-1)

Case $Down

_updown( 1)

EndSwitch

WEnd

func _updown($i)

$Index = _GUICtrlListViewGetCurSel($Listview)

$SelText = _GUICtrlListViewGetItemText($Listview)

If ($SelText = "") Or ($Index + $i < 0) Or ($Index + $i >= _GUICtrlListViewGetItemCount($Listview) ) Then Return

_GUICtrlListViewDeleteItem($Listview, $Index)

_GUICtrlListViewInsertItem($Listview, $Index + $i, $SelText)

ControlListView($Gui, "", $Listview, "Select", $Index + $i, $Index + $i)

endfunc

http://www.autoitscript.com/forum/index.ph...items++listview

I successfully used the up\down function with the included GuictrlcreateListview function, but moving towards _GUICtrlListView_Create it no longer works

This is the current function I am using with the newest _GUICtrlListView_Create function

func _updown($i)
    
    
   $Index = _GUICtrlListView_GetNextItem($Listview)
   $SelText = _GUICtrlListView_GetItemText($Listview, $Index, 3)
    If ($SelText = "") Or ($Index + $i < 0) Or ($Index + $i >= _GUICtrlListView_GetItemCount($Listview)) Then Return
    _GUICtrlListView_DeleteItem(ControlGetHandle("", "", $Listview))    
    _GUICtrlListView_InsertItem($Listview, $Index + $i, $SelText)   
    ControlListView($GUI, "", $Listview, "Select", $Index + $i, $Index + $i)
    
endfunc

Could someone please show me how to get this right?

Again, so sorry for all the questions....

Cheers

Edited by lyledg
Link to comment
Share on other sites

Hi,

I made this function for AutoIt 3.2.8.1, but here is adapted function (+ example) for the latest AutoIt (3.2.10.0):

;Demo for _GUICtrlListView_MoveItems() function.
;Just select one (or more) item, and press one of the buttons (Up or Down).

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

$GUI = GUICreate('Demo for _GUICtrlListView_MoveItems()', 300, 320)

$ListView = GUICtrlCreateListView("Column1|Column2|Column3", 20, 20, 260, 250)
GUICtrlSetStyle($ListView, $LVS_SHOWSELALWAYS+$LVS_REPORT, $LVS_EX_CHECKBOXES+$LVS_EX_FULLROWSELECT)

For $i = 1 To 10
    GUICtrlCreateListViewItem("Item " & $i & "|Item " & $i & " subtext|subtext " & $i, $Listview)
    GUICtrlSetImage(-1, "shell32.dll", 4, 0)
Next

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)

$Up_Button = GUICtrlCreateButton("Up", 20, 280, 32, 32, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", 1)

$Down_Button = GUICtrlCreateButton("Down", 90, 280, 32, 32, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", -2)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Up_Button
            _GUICtrlListView_MoveItems($GUI, $ListView, -1)
            ControlFocus($GUI, "", $ListView)
        Case $Down_Button
            _GUICtrlListView_MoveItems($GUI, $ListView, 1)
            ControlFocus($GUI, "", $ListView)
    EndSwitch
WEnd

;===============================================================================
; Function Name:    _GUICtrlListView_MoveItems()
; Description:      Move selected item(s) in ListView Up or Down.
;
; Parameter(s):     $hWnd               - Window handle of ListView control (can be a Title).
;                   $vListView          - The ID/Handle/Class of ListView control.
;                   $iDirection         - [Optional], define in what direction item(s) will move:
;                                            1 (default) - item(s) will move Next.
;                                           -1 item(s) will move Back.
;                   $sIconsFile         - Icon file to set image for the items (only for internal usage).
;                   $iIconID_Checked    - Icon ID in $sIconsFile for checked item(s).
;                   $iIconID_UnChecked  - Icon ID in $sIconsFile for Unchecked item(s).
;
; Requirement(s):   #include <GuiListView.au3>, AutoIt 3.2.10.0.
;
; Return Value(s):  On seccess - Move selected item(s) Next/Back.
;                   On failure - Return "" (empty string) and set @error as following:
;                                                                  1 - No selected item(s).
;                                                                  2 - $iDirection is wrong value (not 1 and not -1).
;                                                                  3 - Item(s) can not be moved, reached last/first item.
;
; Note(s):          * This function work with external ListView Control as well.
;                   * If you select like 15-20 (or more) items, moving them can take a while :( (second or two).
;
; Author(s):        G.Sandler a.k.a CreatoR
;===============================================================================
Func _GUICtrlListView_MoveItems($hWnd, $vListView, $iDirection=1, $sIconsFile="", $iIconID_Checked=0, $iIconID_UnChecked=0)
    Local $hListView = $vListView
    If Not IsHWnd($hListView) Then $hListView = ControlGetHandle($hWnd, "", $hListView)
    
    Local $aSelected_Indices = _GUICtrlListView_GetSelectedIndices($hListView, 1)
    If UBound($aSelected_Indices) < 2 Then Return SetError(1, 0, "")
    If $iDirection <> 1 And $iDirection <> -1 Then Return SetError(2, 0, "")
    
    Local $iTotal_Items = ControlListView($hWnd, "", $hListView, "GetItemCount")
    Local $iTotal_Columns = ControlListView($hWnd, "", $hListView, "GetSubItemCount")
    
    Local $iUbound = UBound($aSelected_Indices)-1, $iNum = 1, $iStep = 1
    Local $iCurrent_Index, $iUpDown_Index, $sCurrent_ItemText, $sUpDown_ItemText
    Local $iCurrent_Index, $iCurrent_CheckedState, $iUpDown_CheckedState
    
    If ($iDirection = -1 And $aSelected_Indices[1] = 0) Or _
        ($iDirection = 1 And $aSelected_Indices[$iUbound] = $iTotal_Items-1) Then Return SetError(3, 0, "")
    
    ControlListView($hWnd, "", $hListView, "SelectClear")
    
    Local $aOldSelected_IDs[1]
    Local $iIconsFileExists = FileExists($sIconsFile)
    
    If $iIconsFileExists Then
        For $i = 1 To $iUbound
            ReDim $aOldSelected_IDs[UBound($aOldSelected_IDs)+1]
            _GUICtrlListView_SetItemSelected($hListView, $aSelected_Indices[$i], True)
            $aOldSelected_IDs[$i] = GUICtrlRead($vListView)
            _GUICtrlListView_SetItemSelected($hListView, $aSelected_Indices[$i], False)
        Next
        ControlListView($hWnd, "", $hListView, "SelectClear")
    EndIf
    
    If $iDirection = 1 Then
        $iNum = $iUbound
        $iUbound = 1
        $iStep = -1
    EndIf
    
    For $i = $iNum To $iUbound Step $iStep
        $iCurrent_Index = $aSelected_Indices[$i]
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        
        $iCurrent_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iCurrent_Index)
        $iUpDown_CheckedState = _GUICtrlListView_GetItemChecked($hListView, $iUpDown_Index)
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
        
        For $j = 0 To $iTotal_Columns-1
            $sCurrent_ItemText = _GUICtrlListView_GetItemText($hListView, $iCurrent_Index, $j)
            $sUpDown_ItemText = _GUICtrlListView_GetItemText($hListView, $iUpDown_Index, $j)
            
            _GUICtrlListView_SetItemText($hListView, $iUpDown_Index, $sCurrent_ItemText, $j)
            _GUICtrlListView_SetItemText($hListView, $iCurrent_Index, $sUpDown_ItemText, $j)
        Next
        
        _GUICtrlListView_SetItemChecked($hListView, $iUpDown_Index, $iCurrent_CheckedState)
        _GUICtrlListView_SetItemChecked($hListView, $iCurrent_Index, $iUpDown_CheckedState)
        
        If $iIconsFileExists Then
            If $iCurrent_CheckedState = 1 Then
                GUICtrlSetImage(GUICtrlRead($vListView), $sIconsFile, $iIconID_Checked, 0)
            Else
                GUICtrlSetImage(GUICtrlRead($vListView), $sIconsFile, $iIconID_UnChecked, 0)
            EndIf
            
            If $iUpDown_CheckedState = 1 Then
                GUICtrlSetImage($aOldSelected_IDs[$i], $sIconsFile, $iIconID_Checked, 0)
            Else
                GUICtrlSetImage($aOldSelected_IDs[$i], $sIconsFile, $iIconID_UnChecked, 0)
            EndIf
        EndIf
        
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index, 0)
    Next
    
    For $i = 1 To UBound($aSelected_Indices)-1
        $iUpDown_Index = $aSelected_Indices[$i]+1
        If $iDirection = -1 Then $iUpDown_Index = $aSelected_Indices[$i]-1
        _GUICtrlListView_SetItemSelected($hListView, $iUpDown_Index)
    Next
EndFunc

P.S

I think such function should be in the package with _GuiCtrlListView... functions.

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Here is an example with _GUICtrlListView_Create():

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

$GUI = GUICreate('Demo for _GUICtrlListView_MoveItems()', 300, 320)

$ListView = _GUICtrlListView_Create($GUI, "Column1|Column2", 20, 20, 260, 250, $LVS_SHOWSELALWAYS+$LVS_REPORT)
_GUICtrlListView_SetExtendedListViewStyle($ListView, $LVS_EX_CHECKBOXES+$LVS_EX_FULLROWSELECT)

For $i = 0 To 9
    _GUICtrlListView_AddItem($Listview, "Item " & $i+1)
    _GUICtrlListView_AddSubItem($Listview, $i, "SubItem " & $i+1, 1)
Next

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)

$Up_Button = GUICtrlCreateButton("Up", 20, 280, 32, 32, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", 1)

$Down_Button = GUICtrlCreateButton("Down", 90, 280, 32, 32, $BS_ICON)
GUICtrlSetImage(-1, "netcfgx.dll", -2)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Up_Button
            _GUICtrlListView_MoveItems($GUI, $ListView, -1)
            ControlFocus($GUI, "", $ListView)
        Case $Down_Button
            _GUICtrlListView_MoveItems($GUI, $ListView, 1)
            ControlFocus($GUI, "", $ListView)
    EndSwitch
WEnd
Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

I edited my previouse post with the function, fixed some issues and added comment about usage of $sIconsFile parameter (only for internal usage).

Posted in Example Scripts :D

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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...