Sign in to follow this  
Followers 0
pdaughe

ListView Insert Item

5 posts in this topic

I was experiencing unusual behavior in my script and I finally pinpointed it. I adapted the ListView example from the documentation to demonstrate.

The problem I experience is the inability to obtain the control id of an inserted item. My listview has only one column; the helpfile example has three columns, which I left in the adapted code below. Note the insertion of item "Insert|1|2" and the result in the list. This seems like a problem also, but is NOT the problem I have since my list has only one column.

My problem is not being able to obtain the control id of the inserted item. Shouldn't this work?

Thanks in advance for your help.

Paul

#include <GUIConstants.au3>
#include <GUIListView.au3>

GUICreate("listview items",220,250, 100,200,-1,$WS_EX_ACCEPTFILES)
GUISetBkColor (0x00E0FFFF)  ; will change background color

$listview = GUICtrlCreateListView ("col1  |col2|col3  ",10,10,200,150, _
            BitOr ($LVS_NOSORTHEADER, $LVS_NOCOLUMNHEADER, $LVS_SINGLESEL, $LVS_SORTASCENDING), _
            BitOr ($LVS_EX_TRACKSELECT, $LVS_EX_FULLROWSELECT))
$button = GUICtrlCreateButton ("Value?",75,170,70,20)
$item1=GUICtrlCreateListViewItem("item1|col12|col13",$listview)
$item2=GUICtrlCreateListViewItem("item2|col22|col23",$listview)
$item3=GUICtrlCreateListViewItem("item3|col32|col33",$listview)
$input1=GUICtrlCreateInput("",20,200, 150)
GUICtrlSetState(-1,$GUI_DROPACCEPTED)   ; to allow drag and dropping
GUISetState()
;                                           ;Insert item in list (sort order)
$I = _GUICtrlListViewInsertItem ($listview, -1, "Insert|1|2")

;                                           ;Select new item and give it focus
_GUICtrlListViewSetItemSelState ($listview, $I, 1, 1)

$J = _GUICtrlListViewGetCurSel ($listview)  ;Get index of current selection 

If $I <> $J Then MsgBox (0, "Indices", "I = " & $I & ", J = " & $J)

;                                           ;Get text of currently selected item
$SelectItemText = _GUICtrlListViewGetItemText ($listview)

MsgBox (0, "GetItemText", $SelectItemText)

$Control_ID = GUICtrlRead ($listview)       ;This statement fails -- why?

If Not $Control_ID Then MsgBox (0, "GetItemText", "Unable to retrieve control id of inserted item")

Do
  $msg = GUIGetMsg ()
     
   Select
      Case $msg = $button
         MsgBox(0,"listview item",GUICtrlRead(GUICtrlRead($listview)),2)
      Case $msg = $listview
         MsgBox(0,"listview", "clicked="& GUICtrlGetState($listview),2)
   EndSelect
Until $msg = $GUI_EVENT_CLOSE

Share this post


Link to post
Share on other sites



I was experiencing unusual behavior in my script and I finally pinpointed it. I adapted the ListView example from the documentation to demonstrate.

The problem I experience is the inability to obtain the control id of an inserted item. My listview has only one column; the helpfile example has three columns, which I left in the adapted code below. Note the insertion of item "Insert|1|2" and the result in the list. This seems like a problem also, but is NOT the problem I have since my list has only one column.

My problem is not being able to obtain the control id of the inserted item. Shouldn't this work?

Thanks in advance for your help.

Paul

No, The insert is done via dllcall so the internal control id is not created by autoit.


SciTE for AutoItDirections for Submitting Standard UDFs

 

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

 

Share this post


Link to post
Share on other sites

Thank you GaFrost. It seems a predicament:

1) if I use ListViewInsertItem I can get the index of an item added to the list.

2 If I use CreateListViewItem, I can get the control id of the inserted item.

But most of the listview functions required the list index, particularly SetItemState.

If I use CreateListViewItem, there seems to be no way to easily get the index of the added item (I suppose one could search for it, but this seems inefficient).

If I use InsertItem, I have no way to get the control id.

My script provides the ability to add items to the list and to delete or rename existing items. The delete and rename functions change the text color of the item to be renamed/deleted. This works fine, unless an attempt is made to delete or rename an item added to the list in the same session.

One of the great advantages of the listview control over the list control is the ability to get the control id (and thus change the color) of a list item. The way it works now, this is an advantage for only a static list if my understanding is correct?

Of course I would appreciate any suggestions you have.

Sincerely,

Paul

Share this post


Link to post
Share on other sites

_GUICtrlListViewFindItem

Thanks Gary -- I figured I had two choices: rebuild the list or add the item (obtaining the control id) and then searching for it to obtain the index. I chose the latter and used ListViewFindItem. Other people reading this post may realize its not the most efficient, but in my case, and my experience in general, AutoIt is blazingly fast on today's machine. Granted, I only have about 200 items max in the list, but the function is INSTANT.

Thanks again for your help.

Paul

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  
Followers 0