Jump to content
Sign in to follow this  
ShikiPiki

Getting Index of Item selected in list

Recommended Posts

ShikiPiki

I was wondering if it's possible to get the index of the item selected in a list using GUICtrlRead.

What I tried:

$itemSelected = GUICtrlRead($itemList,1) ;I've tried this with advanced  = 0 and = 1
ConsoleWrite($itemSelected)
What it returned:

Product 1.

What I want it to return:

0

Another method i tried was:

$itemSelected = _GUICtrlListView_GetSelectedIndices($itemList)
ConsoleWrite($itemSelected)
What it returned:

nothing

I created the list using GUICtrlCreateList, is the reason why the second method returns nothing is because i'm not using _GUICtrlListView_Create?


Thanks! -ShikiPiki

Share this post


Link to post
Share on other sites
BrewManNH

ListViews and Listboxes are very different animals, the functions won't work between the two. Perhaps _GUICtrlListBox_GetSelItemsText  would work for what you're looking to do.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
ShikiPiki

ListViews and Listboxes are very different animals, the functions won't work between the two. Perhaps _GUICtrlListBox_GetSelItemsText  would work for what you're looking to do.

I tried implementing this and though it seems in the help file this is what i'm looking for, when i run it it seems to return nothign and gets stuck in a lull or something.

 

$itemSelected = GUICtrlRead($itemList)
ConsoleWrite($itemSelected)
Returns:

Product 1.

When pressing Cancel or the close button GUI properly closes.

 

$itemSelected = _GUICtrlListBox_GetSelItems($itemList);changing only this line in the entire code
ConsoleWrite($itemSelected)
Returns:

Nothing

When pressing Cancel ot the close button GUI does not properly close and must be terminated in either program management or the tools bar.

Edit: Maybe this affects the overall results but right after ConsoleWrite I delete the item selected. However, to my understanding this is done in chronological order and should not affect the overall results.

Please let me know if anything is unclear or you wish for more information.

Thanks for all your support

Edited by ShikiPiki

Thanks! -ShikiPiki

Share this post


Link to post
Share on other sites
Melba23

ShikiPiki,

 

$itemSelected = _GUICtrlListBox_GetSelItems($itemList);changing only this line in the entire code
ConsoleWrite($itemSelected)
Returns:

Nothing

And if you look in the Help file, what datatype does it say is returned by _GUICtrlListBox_GetSelItems? And will that datatype be displayed in a MsgBox? :huh:

You need to read about the functions suggested for yourself, not just put them blindly into your code. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

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
ShikiPiki

ShikiPiki,

 

And if you look in the Help file, what datatype does it say is returned by _GUICtrlListBox_GetSelItems? And will that datatype be displayed in a MsgBox? :huh:

You need to read about the functions suggested for yourself, not just put them blindly into your code. ;)

M23

I'm assuming this means i can't just say consolewrite for arrays and have to indicate which index in the array i want displayed?

I also tried coppying their format exactly, and just replacing the variable as my own

$aItems = _GUICtrlListBox_GetSelItems($itemList)
For $iI = 1 To $aItems[0]
     If $iI > 1 Then $sItems &= ", "
     $sItems &= $aItems[$iI]
Next
MsgBox(4160, "Information", "Items Selected: " & $sItems)
The message box just ends up saying "Items Selected: " Edited by ShikiPiki

Thanks! -ShikiPiki

Share this post


Link to post
Share on other sites
Melba23

ShikiPiki,

This works fine for me:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>

$hGUI = GUICreate("Test", 500, 500)
$cList = GUICtrlCreateList("", 10, 10, 200, 300, BitOR($GUI_SS_DEFAULT_LIST, $LBS_EXTENDEDSEL))
GUICtrlSetData($cList, "|Item 0|Item 1|Item 2|Item 3|Item 4")
$cButton = GUICtrlCreateButton("Read", 10, 400, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            $aItems = _GUICtrlListBox_GetSelItems($cList)
            If $aItems[0] = 0 Then
                $sItems ="No Sel"
            Else
                $sItems = ""
                For $i = 1 To $aItems[0]
                     If $i > 1 Then $sItems &= ", "
                     $sItems &= $aItems[$i]
                Next
            EndIf
            MsgBox(4160, "Information", "Items Selected: " & $sItems)
    EndSwitch

WEnd
How about you? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

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
ShikiPiki

ShikiPiki,

This works fine for me:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>

$hGUI = GUICreate("Test", 500, 500)
$cList = GUICtrlCreateList("", 10, 10, 200, 300, BitOR($GUI_SS_DEFAULT_LIST, $LBS_EXTENDEDSEL))
GUICtrlSetData($cList, "|Item 0|Item 1|Item 2|Item 3|Item 4")
$cButton = GUICtrlCreateButton("Read", 10, 400, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            $aItems = _GUICtrlListBox_GetSelItems($cList)
            If $aItems[0] = 0 Then
                $sItems ="No Sel"
            Else
                $sItems = ""
                For $i = 1 To $aItems[0]
                     If $i > 1 Then $sItems &= ", "
                     $sItems &= $aItems[$i]
                Next
            EndIf
            MsgBox(4160, "Information", "Items Selected: " & $sItems)
    EndSwitch

WEnd
How about you? :)

M23

Ah, you know what was the issue? I had to include the BitOR($GUI_SS_DEFAULT_LIST, $LBS_EXTENDEDSEL) when creating my list. How come this is the case though? I thought that these just set it so i can select multiple items, shouldnt it work still if I only wanted to select one item?


Thanks! -ShikiPiki

Share this post


Link to post
Share on other sites
Melba23

ShikiPiki,

I thought that these just set it so i can select multiple items, shouldnt it work still if I only wanted to select one item?

Obviously not. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

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  

×