Jump to content

GUICtrlRead() on a ListView


nitro322
 Share

Recommended Posts

I'm having a problem getting predictable results from GUICtrlRead on a ListView. According to the documentation, GUICtrlRead($ListViewControlID) should give me the "Control identifier (controlID) of the selected ListViewItem". This seems to give me an incremental value for each item selected, but the index does not start at 0. Eg, selecting the first ListViewItem will give me a 8, the second a 9, etc. Not only is it starting at an odd number, it's also changed on my twice, as it originally began at 6, then 7, now 8.

I'm sure I'm just doing something wierd in my code or simply not interpreting the results correctly, but not being able to reliably predict the base value makes it very difficult to perform operations on selected elements.

Here my code.

; Build GUI
$basewidth = 280
$window = GUICreate("Update Drivers", $basewidth + 15, 55 + $height)
$header = GUICtrlCreateLabel("Select a device.", 5, 5, 215, 15)
$list = GUICtrlCreateListView('Host|Device', 5, 25, $basewidth + 5, $height)
$view = GUICtrlCreateButton("View Selected", ($basewidth + 15) / 2 - 140, 30 + $height, 80, 20)
$viewall = GUICtrlCreateButton("View All", ($basewidth + 15) / 2 - 40, 30 + $height, 80, 20)
$next = GUICtrlCreateButton("Next Category", ($basewidth + 15) / 2 + 60, 30 + $height, 80, 20)

; Populate list
for $i = 0 to ubound($drivers)-1
    GUICtrlCreateListViewItem($drivers[$i][0] & "|" & $drivers[$i][1], $list)
next

; Set GUI properties
GUICtrlSendMsg($list, 0x101E, 0, 7 * $maxhost)
GUICtrlSendMsg($list, 0x101E, 1, 7 * $maxdev)
GUICtrlSetFont($header, -1, 1200)
GUICtrlSetState($view, $GUI_DEFBUTTON)
GUICtrlSetState($list, $GUI_FOCUS)
ControlListView("Update Drivers", "", $list, "Select", 0)
msgbox(0, 'Test', guictrlread($list))
exit

$drivers[][] is created earlier in the script. I'd expect the msgbox() at the end to give me a 0 since that's the elemented I selected, but instead it gives me an 8. What am I doing wrong?

Thanks.

Link to comment
Share on other sites

If your using the latest beta, try looking in the User Defined Functions section of the help and look at GuiListView Management

The controls are assigned ids when created, not always starting the same, but the indexing is the same everytime.

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

The issue is that beta _GUICtrlListView...() management functions work on the index of the List View items. If nitro322 wants to use functions like GuiCtrlSetBkColor(), GuiCtrlSetColor(), GuiCtrlSetImage() and GuiCtrlSetFont(), he needs the ids of the controls not the index.

The only way to get the ids is to store the return value of GuiCtrlCreateListViewItem(). This method may not be feasible when you're adding/deleting List View items through different functions. That way knowing the ids of List View items in a List View is not predictable.

Can you advise on any method/function which will return the ids of the current List View items in a List View?

EDIT: Spelling

Edited by tonedeaf
Link to comment
Share on other sites

The issue is that beta _GUICtrlListView...() management functions work on the index of the List View items. If nitro322 wants to use functions like GuiCtrlSetBkColor(), GuiCtrlSetColor(), GuiCtrlSetImage() and GuiCtrlSetFont(), he needs the ids of the controls not the index.

The only way to get the ids is to store the return value of GuiCtrlCreateListViewItem(). This method may not be feasible when you're adding/deleting List View items through different functions. That way knowing the ids of List View items in a List View is not predictable.

Can you advise on any method/function which will return the ids of the current List View items in a List View?

EDIT: Spelling

What the user is doing will work, matter of fact working as coded.

#include <GuiConstants.au3>

Local $drivers[3][2]
$drivers[0][0] = "mine"
$drivers[0][1] = "drive"
$drivers[1][0] = "yours"
$drivers[1][1] = "drive"
$drivers[2][0] = "test"
$drivers[2][1] = "drive"
$height = 200
; Build GUI
$basewidth = 280
$window = GUICreate("Update Drivers", $basewidth + 15, 55 + $height)
$header = GUICtrlCreateLabel("Select a device.", 5, 5, 215, 15)
$list = GUICtrlCreateListView('Host|Device', 5, 25, $basewidth + 5, $height)
$view = GUICtrlCreateButton("View Selected", ($basewidth + 15) / 2 - 140, 30 + $height, 80, 20)
$viewall = GUICtrlCreateButton("View All", ($basewidth + 15) / 2 - 40, 30 + $height, 80, 20)
$next = GUICtrlCreateButton("Next Category", ($basewidth + 15) / 2 + 60, 30 + $height, 80, 20)

; Populate list
for $i = 0 to ubound($drivers)-1
    GUICtrlCreateListViewItem($drivers[$i][0] & "|" & $drivers[$i][1], $list)
next

; Set GUI properties
;~ GUICtrlSendMsg($list, 0x101E, 0, 7 * $maxhost)
;~ GUICtrlSendMsg($list, 0x101E, 1, 7 * $maxdev)
GUICtrlSetFont($header, -1, 1200)
GUICtrlSetState($view, $GUI_DEFBUTTON)
GUICtrlSetState($list, $GUI_FOCUS)
GUISetState()
ControlListView("Update Drivers", "", $list, "Select", 0)
msgbox(0, 'Test', guictrlread($list))
exit

ListViewControl identifier (controlID) of the selected ListViewItem. 0 means no item is selected

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Thanks for the replies. I'm using the stable, not the beta version, so I don't have any UDFs concerning GUI controls (don't think so, anyway). I'm also not trying to do anything fancy with the ListView items; I just need a predictable way to determine which one is selected. As you can see in the could example I'm populating the list view with elements from two columns of an array. There's also a third column that contains additional information that I need to get when I select an element. This is why I need some way to map to an index value, since the array indexes are 0, 1, etc.

I've considered tonedeaf's idea of storing the ListViewItem control ID, but then, as far as I can see, I'd have the same problem I have now of trying to determine which item is selected in order to get the control ID.

gafrost, you said that my method should work, but I'm missing some piece to tie it all together. I tried testing your code snippet, but it seems to return the same starting value as mine; eg, 8 when the first item is selected, 9 for the second, etc. I still need some way to map this back to the index value.

I originally accomplished this by just subtracting what I determined to be the base number. So, $index = GUICtrlRead($list)-8. However, as I mentioned in my first post, this base value has already changed on me twice since I began coding this program, and each time I had to change that function. I'd really, really like to come up with a more robust solution for this that could handle any future changes.

Thanks again for the assistance.

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...