Jump to content

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

Recommended Posts

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 . 

Edit: Only 1 function does not work with single selection box.

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>
#include <Array.au3>

Local $singlesel = 0, $iMsgBoxAnswer = 0

;MsgBox features: Title=Yes, Text=Yes, Buttons=Yes and No, Icon=Question, Modality=Task Modal

$iMsgBoxAnswer = MsgBox(8228, "Choose Listbox selecton type", "Yes for single, No for multi selection box")
If $iMsgBoxAnswer = 6 Then $singlesel = 1 ;Yes

Local $BL_1,$BL_2,$BR_1,$BR_2,$BR_3,$BR_4,$BR_5,$BR_6

Global $hForm1 = GUICreate("Listbox test", 349, 287)

If $singlesel = 1 Then
    $LB_2 = GUICtrlCreateList("", 179, 40, 157, 244, BitOR($LBS_NOTIFY, $WS_HSCROLL, $WS_VSCROLL, $LBS_DISABLENOSCROLL))
    $BR_3 = GUICtrlCreateButton("Reverse Sel", 272, 22, 68, 17)

$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_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)        ;Get selected items
    Local $i, $slb = 0, $y, $x
    If $aSel[0] = 0 Then            ;If the array is empty, there is no selection, or it is a single selection listbox
        For $x = 0 To _GUICtrlListBox_GetCount($hLB_ID) - 1
            $y = _GUICtrlListBox_GetSel($hLB_ID, $x)
            If $y = True Then
                $slb = 1
                _GUICtrlListBox_DeleteString($hLB_ID, $x) ;Perform a delete on single sel. LB

    If $slb = 0 Then
        For $i = $aSel[0] To 1 Step -1                    ;Loop backwards and delete the selected items
            _GUICtrlListBox_DeleteString($hLB_ID, $aSel[$i])
EndFunc   ;==>Listbox_DeleteSelectedItems

Func Listbox_ClearSelection($hLB_ID)
    ;Removes the selection from multi and single selection ListBox
    Local $aSel = _GUICtrlListBox_GetSelItems($hLB_ID)            ;Code from Melba23 - Autoit Forum
    Local $slb, $x, $y
    If $aSel[0] = 0 Then
        _GUICtrlListBox_SetCurSel($hLB_ID, -1)
        $slb = 1

    If $slb = 0 Then
        For $i = 1 To $aSel[0]
            _GUICtrlListBox_SetSel($hLB_ID, $aSel[$i], False)
EndFunc   ;==>Listbox_ClearSelection

Func Listbox_ReverseSelection($hLB_ID)
    ;Logically, this function works only on multi-selection listboxes
    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  Move Multi/Single item in a ListBox
    ;$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, $y, $slb = 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

    If $aSel[0] = 0 Then
        $y = _GUICtrlListBox_GetCurSel($hLB_ID)
        If $y > -1 Then
            _ArrayAdd($aSel, $y)
            $aSel[0] = 1
            $slb = 1

    ;WinSetTitle($hGUI, "", $aSel[0])                   ;Debugging info

        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
                    If $slb = 0 Then
                        _GUICtrlListBox_SetSel($hLB_ID, $aSel[$i] - 1, 1)
                        _GUICtrlListBox_SetCurSel($hLB_ID, $aSel[$i] - 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
                    If $slb = 0 Then
                        _GUICtrlListBox_SetSel($hLB_ID, $aSel[$i] + 1, 1)
                        _GUICtrlListBox_SetCurSel($hLB_ID, $aSel[$i] + 1)
            Return $m
    Return -1
EndFunc   ;==>Listbox_ItemMoveUD


Edited by Dan_555
Updated code, now working with single selection boxes too

Some of my script sourcecode

Link to post
Share on other sites
  • Dan_555 changed the title to Listbox functions (Clear, Delete, Invert, Move Selection)

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.26 contains 214 functions divided into these topics:
      window, desktop and monitor GUI, mouse and color GUI controls including graphical buttons (jpg, png) GUI numberbox controls for integer, real, binary and hexadecimal input 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 Fritterandwaste
      I am a bit confused about how to change the contents of one listbox item. I have been trying to use _GuiCtrlIListbox_GetItemData and _GuiCtrlIListbox_SetItemData. Is this correct? I have tried using _GuiCtrlIListbox_GetItemText also. I have code as follows that is intended to replace the first 4 characters of the entry with 5 dashes :
      $sLbxEntry = _GuiCtrlListbox_GetItemData($gLbxPlayList, $iLstIdx) FnuDebug("Listbox entry no: " & $iLstIdx & ", Listbox error = " & @error & ", Content: " & $sLbxEntry) $sLbxEntry = "-----" & StringMid($sLbxEntry, 5) _GUICtrlListBox_SetItemData($gLbxPlayList, $iLstIdx, $sLbxEntry) ;results of debug: ;DEBUG: Listbox entry no: 3, Listbox error = 0, Content: 0 It would appear that the first line of code is returning "0" yet that listbox entry is clearly populated with text. I suspect I am misunderstanding something very basic?
    • By UE_morf_boon
      I have a program that has a specific function that renames the computer.
      The entire program should be executed only on behalf of the current user, because some registry keys in the HKCU are being changed.
      The current user does not have administrator rights, so #Requireadmin is not suitable as a solution.
      The administrator accounts on the computers are different, so "RunAs" is not suitable.
      Is there a way to trigger a UAC, when running a function?
      The function code, if you need:
      Func _RenamePC($Input2) Local $Name = GUICtrlRead($Input2) $objWMIService = ObjGet("winmgmts:\root\cimv2") For $objComputer In $objWMIService.InstancesOf("Win32_ComputerSystem") $oReturn = $objComputer.rename($Name) Next EndFunc  
    • By ahha
      I seem to have lost my sense of how the Global scope works.  In this example $p is Global but I can't seem to set $p in function __One and then pass that version of $p to function __Two.  I thought you could set a Global to a new value from within a function and it would affect the Global value. 
      I've stared and played with this code for over an hour and I'm missing something really basic.  Help appreciated.
      #AutoIt3Wrapper_run_debug_mode=Y ; use this to debug in console window <--- LOOK Global $p = 999 MsgBox(0, "DEBUG", "MAIN - $p = '" & $p & "'") __One() Exit Func __One() ;Global $p ;this seems to make no difference - comment it out to see - how does one affect the Global in a function? MsgBox(0, "DEBUG", "Entering Func __One() - $p = '" & $p & "'") For $p = 1 to 3 MsgBox(0, "DEBUG", "IN Func __One() - $p = '" & $p & "'") __Two() Next EndFunc Func __Two() MsgBox(0, "DEBUG", "Entering __Two() $p = '" & $p & "'") EndFunc  
    • By meety
      My au3 script program needs administrator privileges on win10 to be executed correctly, because it needs to be added to the boot---Script code:RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", @ScriptName, "REG_SZ", @ScriptFullPath)
      I need to let the program automatically get administrator privileges, so I use a function of #RequireAdmin. Although the program can be added to the boot, but does not execute after system boot up? why is that?
      What should I do?
      I found that after using the #RequireAdmin function, the program cannot be executed after system boot up.
      An example is given below. after it is compiled, it will not be executed after system boot up in the following two ways.
      1. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\
      2. RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", @ScriptName, "REG_SZ", @ScriptFullPath)
      why is that?
      #include <MsgBoxConstants.au3>
      Func test()
          MsgBox(0, "", "this is test message")
      EndFunc   ;==>test
  • Create New...