Sign in to follow this  
Followers 0
Nanaki

Moving ListViewItems up and down

6 posts in this topic

Topic says it all. :)

I want to create a ListView with two buttons "up" and "down". Clicking them will move the selected ListViewItem up or down. Is it possible?

Thanks in advance,

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I did something like that in this tool (see screenshot) :

http://www.autoitscript.com/forum/index.php?showtopic=26457

This is an example for 4 columns (the first column is a checkbox)

Looks like this :

Func _upmove()
    if _GUICtrlListViewGetSelectedCount ($mylist) = 1 and _GUICtrlListViewGetSelectedIndices ($mylist) <> 0  then
        $curindex = _GUICtrlListViewGetSelectedIndices ($mylist)
        $col0 = _GUICtrlListViewGetCheckedState($mylist,$curindex-1)
        $col1 = _GUICtrlListViewGetItemText ($mylist,$curindex-1,1)
        $col2 = _GUICtrlListViewGetItemText ($mylist,$curindex-1,2)
        $col3 = _GUICtrlListViewGetItemText ($mylist,$curindex-1,3)
        _GUICtrlListViewSetCheckState ($mylist,$curindex-1,_GUICtrlListViewGetCheckedState($mylist,$curindex+0))
        _GUICtrlListViewSetItemText ($mylist,$curindex-1,1,_GUICtrlListViewGetItemText ($mylist,$curindex+0,1))
        _GUICtrlListViewSetItemText ($mylist,$curindex-1,2,_GUICtrlListViewGetItemText ($mylist,$curindex+0,2))
        _GUICtrlListViewSetItemText ($mylist,$curindex-1,3,_GUICtrlListViewGetItemText ($mylist,$curindex+0,3))
;
        _GUICtrlListViewSetCheckState($mylist,$curindex-0,$col0)
        _GUICtrlListViewSetItemText ($mylist,$curindex-0,1,$col1)
        _GUICtrlListViewSetItemText ($mylist,$curindex-0,2,$col2)
        _GUICtrlListViewSetItemText ($mylist,$curindex-0,3,$col3)
;
        _GUICtrlListViewSetItemSelState ($mylist,$curindex-1,1)
        _GUICtrlListViewSetItemSelState ($mylist,$curindex-0,0)
    EndIf
EndFunc;==>_upmove
;
Func _downmove()
    if _GUICtrlListViewGetSelectedCount ($mylist) = 1 and _GUICtrlListViewGetSelectedIndices ($mylist) <> _GUICtrlListViewGetItemCount($mylist)-1 then
        $curindex = _GUICtrlListViewGetSelectedIndices ($mylist)
        $col0 = _GUICtrlListViewGetCheckedState($mylist,$curindex+1)
        $col1 = _GUICtrlListViewGetItemText ($mylist,$curindex+1,1)
        $col2 = _GUICtrlListViewGetItemText ($mylist,$curindex+1,2)
        $col3 = _GUICtrlListViewGetItemText ($mylist,$curindex+1,3)
        _GUICtrlListViewSetCheckState ($mylist,$curindex+1,_GUICtrlListViewGetCheckedState($mylist,$curindex+0))
        _GUICtrlListViewSetItemText ($mylist,$curindex+1,1,_GUICtrlListViewGetItemText ($mylist,$curindex+0,1))
        _GUICtrlListViewSetItemText ($mylist,$curindex+1,2,_GUICtrlListViewGetItemText ($mylist,$curindex+0,2))
        _GUICtrlListViewSetItemText ($mylist,$curindex+1,3,_GUICtrlListViewGetItemText ($mylist,$curindex+0,3))
;
        _GUICtrlListViewSetCheckState($mylist,$curindex-0,$col0)
        _GUICtrlListViewSetItemText ($mylist,$curindex-0,1,$col1)
        _GUICtrlListViewSetItemText ($mylist,$curindex-0,2,$col2)
        _GUICtrlListViewSetItemText ($mylist,$curindex-0,3,$col3)
;
        _GUICtrlListViewSetItemSelState ($mylist,$curindex+1,1)
        _GUICtrlListViewSetItemSelState ($mylist,$curindex-0,0)
    EndIf
EndFunc;==>_downmove
Edited by Gyzmok

D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper

Share this post


Link to post
Share on other sites

i fink i simplified it a bit.

Case $msg = $up
                
                If _GUICtrlListViewGetSelectedCount($listview) = 1 And _GUICtrlListViewGetSelectedIndices($listview) + 1 > 1 Then
                    $curindex = _GUICtrlListViewGetSelectedIndices($listview)
                    For $x = 0 To _GUICtrlListViewGetSubItemsCount($listview)
                        $x_i = _GUICtrlListViewGetItemText($listview, $curindex - 1, $x)
                        _GUICtrlListViewSetItemText($listview, $curindex - 1, $x, _GUICtrlListViewGetItemText($listview, $curindex + 0, $x))
                        _GUICtrlListViewSetItemText($listview, $curindex - 0, $x, $x_i)
                    Next
                    _GUICtrlListViewSetItemSelState($listview, $curindex - 1, 1)
                    _GUICtrlListViewSetItemSelState($listview, $curindex - 0, 0)
                EndIf
                
            Case $msg = $down
                
                If _GUICtrlListViewGetSelectedCount($listview) = 1 And _GUICtrlListViewGetSelectedIndices($listview) + 1 < _GUICtrlListViewGetItemCount($listview) Then
                    $curindex = _GUICtrlListViewGetSelectedIndices($listview)
                    For $x = 0 To _GUICtrlListViewGetSubItemsCount($listview)
                        $x_i = _GUICtrlListViewGetItemText($listview, $curindex + 1, $x)
                        _GUICtrlListViewSetItemText($listview, $curindex + 1, $x, _GUICtrlListViewGetItemText($listview, $curindex + 0, $x))
                        _GUICtrlListViewSetItemText($listview, $curindex - 0, $x, $x_i)
                    Next
                    _GUICtrlListViewSetItemSelState($listview, $curindex + 1, 1)
                    _GUICtrlListViewSetItemSelState($listview, $curindex - 0, 0)
                EndIf

Share this post


Link to post
Share on other sites

I did not test that, but how about this:

- get text of selected item

- get index of selected item

- delete selected item

- insert new item with text at desired position

The functions to do this exist:

_GUICtrlListViewGetItemText($h_listview[, $i_Item=-1[, $i_SubItem=-1]])

_GUICtrlListViewGetSelectedIndices($h_listview[, $i_ReturnType=0]))

_GUICtrlListViewDeleteItem($h_listview, $i_index)

_GUICtrlListViewInsertItem($h_listview, $i_index, $s_text)

Share this post


Link to post
Share on other sites

Hi,

Here is the quickly, and i think most effective way to do this:

#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
            $Index = _GUICtrlListViewGetCurSel($Listview)
            $SelText = _GUICtrlListViewGetItemText($Listview)
            If $SelText = "" Or $Index = 0 Then ContinueLoop
            _GUICtrlListViewDeleteItem($Listview, $Index)
            _GUICtrlListViewInsertItem($Listview, $Index-1, $SelText)
            ControlListView($Gui, "", $Listview, "Select", $Index-1, $Index-1)
        Case $Down
            $Index = _GUICtrlListViewGetCurSel($Listview)
            $SelText = _GUICtrlListViewGetItemText($Listview)
            If $SelText = "" Or $Index+1 = _GUICtrlListViewGetItemCount($Listview) Then ContinueLoop
            _GUICtrlListViewDeleteItem($Listview, $Index)
            _GUICtrlListViewInsertItem($Listview, $Index+1, $SelText)
            ControlListView($Gui, "", $Listview, "Select", $Index+1, $Index+1)
    EndSwitch
WEnd

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

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

Share this post


Link to post
Share on other sites

Hi,

Here is the quickly, and i think most effective way to do this:

Thanks, thats exactly what I thought about. This was not on my to do list, but nice to have. Therefore I must admit that I will use your code.

One little change: since I dont like large GUI message loops, I put the 6 lines of interest in a function:

#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

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