Jump to content
Sign in to follow this  

Return The Index Number of A List Control

Recommended Posts

I have a one dimensional array that I'm displaying in a list control and I'm trying to create three functions for the list control. I want to be able to move an item up on the list, down on the list, and remove and item from the list (and the array at the same time). Unless there is a functions that I've overlooked, the best way I know to do this to get the index number of which line is selected on the list, process the appropriate function in the array, then set my list to match my array. The only problem is that I don't know how to get the index number so that I know which line on the list is selected. The only thing I can figure out is that I can get the text of the line that is selected and then compare it to each item in the array until I find a match and just hope that the item selected isn't a duplicated further down on this list.

So the root of the question is, how to I get the index number of the line that is selected in a list control? Any other ideas on how I could streamline the process would be greatly appreciated. Thanks guys!

Share this post

Link to post
Share on other sites


If you were to consider using a ListView rather than a ListBox, then the code I posted here will do just what you want (and a bit more!). :party:

Or if you insist on a ListBox, you can use this: :mellow:

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

$fDblClk = False

Global $sList = "One|Two|Three|Four"

$hGUI = GUICreate("Test", 500, 500)

$hListBox = GUICtrlCreateList("", 10, 10, 200, 200, $LBS_NOTIFY )
$hList_Handle = GUICtrlGetHandle($hListBox)

GUICtrlSetData($hListBox, $sList)


GUIRegisterMsg($WM_COMMAND, "WM_ListBox_DoubleClick")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE

    If $fDblClk = True Then
        $fDblClk = False
        $iIndex = _GUICtrlListBox_GetCaretIndex($hListBox)
        ConsoleWrite($iIndex & @CRLF)


; React to double clicks on listbox
Func WM_ListBox_DoubleClick($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    If BitAND($wParam, 0xFFFF) = $hListBox Then ; LoWOrd = ControlID
        If BitShift($wParam, 16) = 2 Then $fDblClk = True ; HiWord = $LBN_DBLCLK
EndFunc   ;==>MY_WM_COMMAND

I hope that helps. :P


Edit. Added a bit more! :party:

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...