Jump to content

Attempting to read the name of an item from ListView


Recommended Posts

  • Moderators

I am working on updates to a project I recently delivered to a customer, a front-end for Microsoft's SCCM tool. At present, a list of the Application collections they have is retrieved in a 2d array. One column of that array is then used to populate a combo box in the GUI (see screenshot).

The customer would like to change the combo box to a List View, to give a more visual look to the GUI. I can do that, no problem, but am having problems with capturing the text of the highlighted item. I've used the $WM_NOTIFY function in the past for right-clicking on an item, but in this case, the customer would like a helpdesk tech to highlight the name of the collection and then click a button to add a machine to that collection. If I can capture the text of the item, I can query the array for the matching Collection.ID and perform the Add to Collection task no problem. I'm thinking _GUICtrlListView_GetItemText, but you seem to have to hard code the index number in there. Can anyone suggest how to go about capturing the text of the highlighted item? I added a short reproducer below.

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <GuiListView.au3>
 
Local $msg
Local $aArray[5]
   $aArray[0] = "Adobe Acrobat Reader 9"
   $aArray[1] = "Adobe Flash Player 10.3"
   $aArray[2] = "CD AutoRun 1.0"
   $aArray[3] = "Core Utilities"
   $aArray[4] = "Dragon Naturally Speaking 10.0"

GUICreate("Reproducer")
$listview = GUICtrlCreateListView("listview items", 8, 80, 300, 300)
$button = GUICtrlCreateButton("Add PC to collection", 280, 10, 110, 50)
_GUICtrlListView_SetColumn($listview, 0, "", 300, 0)
_GUICtrlListView_ClickItem($listview, 1, "right", False)

   For $i = 1 To UBound($aArray) - 1
     GUICtrlCreateListViewItem($aArray[$i], $listview)
   Next
 

   GUISetState(@SW_SHOW)

   While 1
        $msg = GUIGetMsg()
        Select
          Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
          Case $msg = $button
            MsgBox(0, "", _GUICtrlListView_GetItemText($listview, 1));<---a way to detect the highlighted item.
        EndSelect
    WEnd
   
    GUIDelete()
Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <GuiListView.au3>
Local $msg
Local $aArray[5]
   $aArray[0] = "Adobe Acrobat Reader 9"
   $aArray[1] = "Adobe Flash Player 10.3"
   $aArray[2] = "CD AutoRun 1.0"
   $aArray[3] = "Core Utilities"
   $aArray[4] = "Dragon Naturally Speaking 10.0"
GUICreate("Reproducer")
$listview = GUICtrlCreateListView("listview items", 8, 80, 300, 300)
$button = GUICtrlCreateButton("Add PC to collection", 280, 10, 110, 50)
_GUICtrlListView_SetColumn($listview, 0, "", 300, 0)
_GUICtrlListView_ClickItem($listview, 1, "right", False)
   For $i = 1 To UBound($aArray) - 1
     GUICtrlCreateListViewItem($aArray[$i], $listview)
   Next

   GUISetState(@SW_SHOW)
   While 1
        $msg = GUIGetMsg()
        Select
          Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
          Case $msg = $button
            MsgBox(0, "", _GUICtrlListView_GetItemTextString($listview, -1));<---a way to detect the highlighted item.
        EndSelect
    WEnd
    GUIDelete()

If you use _GUICtrlListView_GetItemTextString you can use -1 as the item #, which will use the currently selected item. You can also use _GUICtrlListView_GetItemTextArray if that makes more sense in your code.

Edited by someone
While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
Share on other sites

  • Moderators

Hi, someone. I thought of that as well. However, in the reproducer above, if I choose -1 the MsgBox is blank. I glazed over the _GUICtrlListView_GetItemTextArray, but will circle back and see if that fits my needs.

Edit: I missed that you are using _GUICtrlListView_GetItemTextString rather than just _GUICtrlListView_GetItemText. That works very well, thanks.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Not to harp on it, but I've banged my head into the wall many times with listviews, so depending on what you need, the following example may be of use.

I generally use _GUICtrlListView_GetSelectedIndices and then take action as needed.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <GuiListView.au3>
Local $msg
Local $aArray[5]
   $aArray[0] = "Adobe Acrobat Reader 9"
   $aArray[1] = "Adobe Flash Player 10.3"
   $aArray[2] = "CD AutoRun 1.0"
   $aArray[3] = "Core Utilities"
   $aArray[4] = "Dragon Naturally Speaking 10.0"
GUICreate("Reproducer")
$listview = GUICtrlCreateListView("listview items", 8, 80, 300, 300)
$button = GUICtrlCreateButton("Add PC to collection", 280, 10, 110, 50)
_GUICtrlListView_SetColumn($listview, 0, "", 300, 0)
_GUICtrlListView_ClickItem($listview, 1, "right", False)
   For $i = 1 To UBound($aArray) - 1
     GUICtrlCreateListViewItem($aArray[$i], $listview)
   Next

   GUISetState(@SW_SHOW)
   While 1
        $msg = GUIGetMsg()
        Select
          Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
  Case $msg = $button
   $array1 = _GUICtrlListView_GetItemTextArray($listview)
   MsgBox(0, "1st", $array1[1]) ;1 for 1st item;<---a way to detect the highlighted item.
   $array2 = _GUICtrlListView_GetSelectedIndices($listview, True)
   MsgBox(0, "Second", _GUICtrlListView_GetItemText($listview, $array2[1]))
    MsgBox(0, "Third", _GUICtrlListView_GetItemTextString($listview, -1));<---a way to detect the highlighted item.
        EndSelect
    WEnd
    GUIDelete()

Sorry formatting is off from the tags.

Edited by someone
While ProcessExists('Andrews bad day.exe')
	BlockInput(1)
	SoundPlay('Music.wav')
	SoundSetWaveVolume('Louder')
WEnd
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...