maniootek

Get ID of selected ListView item (after column sorting)

11 posts in this topic

#1 ·  Posted (edited)

For best understand I prepared code sample

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

$gui1 = GUICreate("test", 220, 220)
$idListview = GUICtrlCreateListView("ID|Value", 5, 5, 215, 150)
$iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES,  $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
_GUICtrlListView_SetExtendedListViewStyle($idListview, $iExListViewStyle)
_GUICtrlListView_RegisterSortCallBack($idListview)

$button = GUICtrlCreateButton("show selected id", 50 , 170,100, 40)
$records = 5
local $aListviewItem[$records]
for $i = 0 to UBound($aListviewItem) -1
      $aListviewItem[$i] = GUICtrlCreateListViewItem($i & "|" & "Value: " & $i, $idListview)
Next


GUISetState(@SW_SHOW, $gui1)
While 1
    $nMsg = GUIGetMsg(1)
    Switch $nMsg[1]
    Case $gui1
      Switch $nMsg[0]
         Case $GUI_EVENT_CLOSE
            Exit
         Case $idListview
             _GUICtrlListView_SortItems($idListView, GUICtrlGetState($idListView))
         Case $button
            msgbox(0,"Selected ID",_GUICtrlListView_GetSelectionMark($idListview))
      EndSwitch
   EndSwitch
WEnd

and picture

post-51823-0-87982500-1425481502_thumb.p

Edited by maniootek

Share this post


Link to post
Share on other sites



#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

$gui1 = GUICreate("test", 220, 220)
$idListview = GUICtrlCreateListView("ID|Value", 5, 5, 215, 150)
$iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES,  $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
_GUICtrlListView_SetExtendedListViewStyle($idListview, $iExListViewStyle)
_GUICtrlListView_RegisterSortCallBack($idListview)

$button = GUICtrlCreateButton("show selected id", 50 , 170,100, 40)
$records = 5
local $aListviewItem[$records]
for $i = 0 to UBound($aListviewItem) -1
      $aListviewItem[$i] = GUICtrlCreateListViewItem($i & "|" & "Value: " & $i, $idListview)
Next


GUISetState(@SW_SHOW, $gui1)
While 1
    $nMsg = GUIGetMsg(1)
    Switch $nMsg[1]
    Case $gui1
      Switch $nMsg[0]
         Case $GUI_EVENT_CLOSE
            Exit
         Case $idListview
             _GUICtrlListView_SortItems($idListView, GUICtrlGetState($idListView))
         Case $button
            msgbox(0,"Selected ID", stringsplit(GuiCtrlRead(GuiCtrlRead($idListView), 1), "|")[1])
      EndSwitch
   EndSwitch
WEnd


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

I'm not sure, but I believe the OP is looking to get the original index number of the item, not the number in the first column. Although I could be wrong. ;)


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

I am an expert at falling on the wrong side of ambiguity.  But so far those two numbers would be the same provided the OP is cool with 0-base.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

The question was vague enough, so you could be right in your solution. :D


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

thank you @boththose

Share this post


Link to post
Share on other sites

For those who didn't get bored by me yet I have got very similar situation but instead of selected I want get checked values

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

$gui1 = GUICreate("test", 220, 220)
$idListview = GUICtrlCreateListView("ID|Value", 5, 5, 215, 150)
$iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES,  $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
_GUICtrlListView_SetExtendedListViewStyle($idListview, $iExListViewStyle)
_GUICtrlListView_RegisterSortCallBack($idListview)

$button = GUICtrlCreateButton("show selected id", 50 , 170,100, 40)
$records = 5
local $aListviewItem[$records]
for $i = 0 to UBound($aListviewItem) -1
      $aListviewItem[$i] = GUICtrlCreateListViewItem($i & "|" & "Value: " & $i, $idListview)
Next


GUISetState(@SW_SHOW, $gui1)
While 1
    $nMsg = GUIGetMsg(1)
    Switch $nMsg[1]
    Case $gui1
      Switch $nMsg[0]
         Case $GUI_EVENT_CLOSE
            Exit
         Case $idListview
             _GUICtrlListView_SortItems($idListView, GUICtrlGetState($idListView))
          Case $button
             for $i = 0 to UBound($aListviewItem) -1
                if _GUICtrlListView_GetItemChecked($idListview, $i) Then
                   msgbox(0,0,"checked: " & $i)
                EndIf
             Next

      EndSwitch
   EndSwitch
WEnd

post-51823-0-38455000-1425502425_thumb.p

Share this post


Link to post
Share on other sites

Same exact thing as was already posted, just get the checked item number the same way as the selected item number, using your latest code.


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

#9 ·  Posted (edited)

Same exact thing as was already posted, just get the checked item number the same way as the selected item number, using your latest code.

No, it will not be same exact thing just because only one item can be selected when mutliple items can be checked. Can't figure out how it should be.

Added:

Also this code:

stringsplit(GuiCtrlRead(GuiCtrlRead($idListView), 1), "|")[1]

are related to selected listviewitem, what about checked item?

Edited by maniootek

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

with an assist from valuater circa 2008

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

$gui1 = GUICreate("test", 220, 220)
$idListview = GUICtrlCreateListView("ID|Value", 5, 5, 215, 150)
$iExListViewStyle = BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES,  $LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES, $LVS_EX_DOUBLEBUFFER)
_GUICtrlListView_SetExtendedListViewStyle($idListview, $iExListViewStyle)
_GUICtrlListView_RegisterSortCallBack($idListview)

$button = GUICtrlCreateButton("show selected id", 50 , 170,100, 40)
$records = 5
local $aListviewItem[$records]
for $i = 0 to UBound($aListviewItem) -1
      $aListviewItem[$i] = GUICtrlCreateListViewItem($i & "|" & "Value: " & $i, $idListview)
Next


GUISetState(@SW_SHOW, $gui1)
While 1
    $nMsg = GUIGetMsg(1)
    Switch $nMsg[1]
    Case $gui1
      Switch $nMsg[0]
         Case $GUI_EVENT_CLOSE
            Exit
         Case $idListview
             _GUICtrlListView_SortItems($idListView, GUICtrlGetState($idListView))
          Case $button
             For $x = 0 To 5
                If _GUICtrlListView_GetItemChecked($idListView, $x - 1) Then
                    MsgBox(0, "listview item", _GUICtrlListView_GetItemTextString($idListView, $x - 1) & "    " & @CRLF & "Line Checked = " & $x - 1, 2)
                EndIf
             Next
        EndSwitch
   EndSwitch
WEnd
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

thank you again @boththose

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