Jump to content

Listbox functions (Clear, Delete, Invert, Move Selection)

Recommended Posts

Posted (edited)

Hi, here are few functions for the ListBox.

I have searched the forum, but most of the functions are for listview, so i took one example code from melba23 (clear selection) and

wrote few more functions. (Because my current project needs them).


These functions work only on a Multi-selection ListBox .

The functions do: Clear Selection, Delete Selected items, Invert Selection, Move selected items up and down.

 The example code has 2 Listboxes. The selected items on the left ListBox can be moved up and down. The right Listbox has buttons for the other functions.

#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <WindowsConstants.au3>

Global $hForm1 = GUICreate("Listbox test", 349, 287)
$BL_1 = GUICtrlCreateButton("Up", 20, 3, 35, 18)
$BL_2 = GUICtrlCreateButton("Down", 60, 3, 35, 18)
$BR_1 = GUICtrlCreateButton("Up", 200, 3, 35, 18)
$BR_2 = GUICtrlCreateButton("Down", 240, 3, 35, 18)
$BR_3 = GUICtrlCreateButton("Reverse Sel", 272, 22, 68, 17)
$BR_4 = GUICtrlCreateButton("Clear Sel", 217, 22, 52, 17)
$BR_5 = GUICtrlCreateButton("Delete", 175, 22, 40, 17)
$BR_6 = GUICtrlCreateButton("Populate", 290, 3, 50, 18)

For $x = 0 To 50
    If $x <= 10 Then GUICtrlSetData($LB_1, $x & " test", 0)
    GUICtrlSetData($LB_2, $x & " Test", 0)

While 1

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
        Case $BL_1
            $a = Listbox_ItemMoveUD($LB_1, -1)
            If $a > -1 Then WinSetTitle($hForm1, "", "Moved items: " & $a)
        Case $BL_2
            $a = Listbox_ItemMoveUD($LB_1, 1)
            If $a > -1 Then WinSetTitle($hForm1, "", "Moved items: " & $a)
        Case $BR_1
            Listbox_ItemMoveUD($LB_2, -1)
        Case $BR_2
            Listbox_ItemMoveUD($LB_2, 1)
        Case $BR_3
        Case $BR_4
        Case $BR_5
        Case $br_6                                  ;Populate
            GUICtrlSetData($LB_2, "") ; Clears the listbox
            For $x = 0 To 50
                GUICtrlSetData($LB_2, $x & " Test", 0)

;note $hLB_ID - is the Listbox id

Func Listbox_DeleteSelectedItems($hLB_ID)
    local $aSel = _GUICtrlListBox_GetSelItems($hLB_ID)
    Local $i
    For $i = $aSel[0] To 1 Step -1
        _GUICtrlListBox_DeleteString($hLB_ID, $aSel[$i])
EndFunc   ;==>Listbox_DeleteSelectedItems

Func Listbox_ClearSelection($hLB_ID)
    Local $aSel = _GUICtrlListBox_GetSelItems($hLB_ID)            ;Code from Melba23 - Autoit Forum
    For $i = 1 To $aSel[0]
        _GUICtrlListBox_SetSel($hLB_ID, $aSel[$i], False)
EndFunc   ;==>Listbox_ClearSelection

Func Listbox_ReverseSelection($hLB_ID)
    Local $i
    Local $aCou = _GUICtrlListBox_GetCount($hLB_ID)
    Local $cSel = _GUICtrlListBox_GetCaretIndex($hLB_ID)    ;Save the caret
    For $i = 0 To $aCou
        _GUICtrlListBox_SetSel($hLB_ID, $i, Not (_GUICtrlListBox_GetSel($hLB_ID, $i)))
    _GUICtrlListBox_SetCaretIndex($hLB_ID, $cSel)            ;Restore the caret
EndFunc   ;==>Listbox_ReverseSelection

Func Listbox_ItemMoveUD($hLB_ID, $iDir = -1)
    ;Listbox_ItemMoveUD - Up/Down - Works only on multiple selection listbox ($LBS_MULTIPLESEL)
    ;$iDir: -1 up, 1 down
    ;Return values -1 nothing to do, 0 nothing moved, >0 performed moves
    Local $iCur, $iNxt, $aCou, $aSel, $i, $m = 0 ;Current, next, Count, Selection, loop , movecount

    $aSel = _GUICtrlListBox_GetSelItems($hLB_ID)            ;Put selected items in an array
    $aCou = _GUICtrlListBox_GetCount($hLB_ID)               ;Get total item count of the listbox

    WinSetTitle ($hForm1,"",$aSel[0])

        Case $iDir = -1                 ;Move Up
            For $i = 1 To $aSel[0]
                If $aSel[$i] > 0 Then
                    $iNxt = _GUICtrlListBox_GetText($hLB_ID, $aSel[$i] - 1)  ;Save the selection index - 1 text
                    _GUICtrlListBox_ReplaceString($hLB_ID, $aSel[$i] - 1, _GUICtrlListBox_GetText($hLB_ID, $aSel[$i])) ;Replace the index-1 text with the index text
                    _GUICtrlListBox_ReplaceString($hLB_ID, $aSel[$i], $iNxt) ;Replace the selection with the saved var
                    $m = $m + 1
            For $i = 1 To $aSel[0]                  ;Restore the selections after moving
                If $aSel[$i] > 0 Then
                    _GUICtrlListBox_SetSel($hLB_ID, $aSel[$i] - 1, 1)
            Return $m
        Case $iDir = 1                  ;Move Down
            If $aSel[0] > 0 Then
                For $i = $aSel[0] To 1 Step -1
                    If $aSel[$i] < $aCou - 1 Then
                        $iNxt = _GUICtrlListBox_GetText($hLB_ID, $aSel[$i] + 1)
                        _GUICtrlListBox_ReplaceString($hLB_ID, $aSel[$i] + 1, _GUICtrlListBox_GetText($hLB_ID, $aSel[$i]))
                        _GUICtrlListBox_ReplaceString($hLB_ID, $aSel[$i], $iNxt)
                        $m = $m + 1
            For $i = $aSel[0] To 1 Step -1          ;Restore the selections after moving
                If $aSel[$i] < $aCou - 1 Then
                    _GUICtrlListBox_SetSel($hLB_ID, $aSel[$i] + 1, 1)
            Return $m
    Return -1
EndFunc   ;==>Listbox_ItemMoveUD


Edited by Dan_555

~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By PeterVerbeek
      This topic give you access to an AutoIt functions library I maintain which is called PAL, Peter's AutoIt Library. The latest version 1.20 contains 197 functions divided into these topics:
      window, desktop and monitor mouse and GUI GUI controls including graphical buttons (jpg, png) logics and mathematics include constants string, xml string and file string dialogues and progress bars data lists: lists, stacks, shift registers and key maps (a.ka. dictionaries) miscellaneous: logging/debugging, process and system info Change log and files section  on the PAL website (SourceForge).
      A lot of these functions were created in the development of Peace, Peter's Equalizer APO Configuration Extension, which is a user interface for the system-wide audio driver called Equalizer APO.
    • By Rabu
      I want to use the winsock overlapped/completion routine method of i/o, but how do I get the pointer to my user function.  I know it is possible because when using the old method of windows messages, the user function name was used instead of a pointer.
    • By topgundcp
      This is my very first post in this forum and am also new with Autoit programming so be easy on me.
      Below is the code that I am trying to get the index of the item selected. No problem getting the text.
      Case $listEdition   ; handle of the list             local $index=0             $item=GUICTRLRead($listEdition)    ; This will return the text in the list             ; ==============   The loop below always fails  ==================             ; Meaning _GUICtrlListView_GetItemSelected($listEdition, $i) always return FALSE. WHY ???????              For $i = 0 To $editionArray[0] - 1   ; The content of the list in an array where editionArray[0] contains total count of items                 If _GUICtrlListView_GetItemSelected($listEdition, $i) Then                     $index = $i                     exitloop                 EndIf              Next              ;===================================================             ConsoleWrite( "Select Edition:     " & $item & "  index: " & $index & @CR) ....... Please look at the comments in the code & the pix attached.
      _GUICtrlListView_GetItemSelected($listEdition, $i) always returns FALSE.
      Another problem is on the list. It also populate  the total count of items from the Array. How can I skip this item from populating to the list ?.
      Please advise and thanks

    • By nacerbaaziz
      hi dears, i have a question please
      am now working for an audio player for the blind users
      and i added an option to open a file from the right click context menu.
      but here there is a small problem, i hope that you can help me to find a solution for it.
      the problem is as follow :
      when i added the problem into the context menu it work with successfuly if the user select just one file.
      but if the user selected more then one file, here the problem
      a multiple sections of the program will open.
      my question is :
      how i can detect the multiple selection from the context menu?
      i know that it so  hard, but i know also that you are harder than it
      for that dears please try to give me an solution for that problem.
      thanks in advance
    • By TheDcoder
      Hello, it has been a long time since I have posted here
      I am working on an AutoIt project where I need to enable to user to interactively choose any point or coordinate on the screen... something like a big overlay where the user can click anywhere on the screen to select that point. @UEZ's screenshot tool may have something similar to what I need:
      I just checked the source code for the tool and I see that the Mark_Area function has a part in selecting the area to screenshot, I tried to figure out how it works but it is simply too complex and long...
      So I was wondering if there were any examples of interactively selecting points on a screen? Maybe an UDF that I can just use in my script to make the whole thing a matter of adding few lines
      Thank you for the replies in advance!
  • Create New...