Jump to content

[NOT SOLVABLE/CLOSED] Get listview item ID with InsertItem


FireFox
 Share

Recommended Posts

Hi,

It sounds quite easy, but I haven't found anything to retrieve a listview item ID.

I though it was under my noze, until I found this :

Unfortunately it's not working (function returning 0), and I'm using the native GUICtrlCreateListView.

As the topic above, I want to retreive the item ID of the inserted item in order to create a context menu on it.

Thanks for anyhelp.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

I think that Native LV items uses ItemParam to hold ControlId. You can use _GUICtrlListView_GetItemParam.

Lars.

Link to comment
Share on other sites

You're right as said in the example of the function from the helpfile :

; Param is the controlId for items created with built-in function

But it's not working, returning 0.

Btw, the function from the topic I mentioned does the same thing.

Br, FireFox.

Link to comment
Share on other sites

In this example from the help file (GUICtrlCreateListViewItem) it does work:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
Example()
Func Example()
Local $listview, $button, $item1, $item2, $item3, $msg
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);,$LVS_SORTDESCENDING)
$button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
$item1 = GUICtrlCreateListViewItem("item2|col22|col23", $listview)
$item2 = GUICtrlCreateListViewItem("............item1|col12|col13", $listview)
$item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
GUICtrlCreateInput("", 20, 200, 150)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping
GUISetState()
Local $hLV = GUICtrlGetHandle( $listview )
MsgBox( 0,"", _GUICtrlListView_GetItemParam($hLV, 0) ) ; <----------------------------------------
GUICtrlSetData($item2, "|ITEM1")
GUICtrlSetData($item3, "||COL33")
GUICtrlDelete($item1)
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
EndFunc ;==>Example

Lars.

Link to comment
Share on other sites

@LarsJ

Ok but in this case it's totally useless (because the control ID is returned by the function creating the item)

I would like this to work with the function _GUICtrlListView_InsertItem (as I said in my first post)

You can see it fails :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
Example()
Func Example()
Local $listview, $button, $item1, $item2, $item3, $msg
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);,$LVS_SORTDESCENDING)
$button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
$item2 = GUICtrlCreateListViewItem("............item1|col12|col13", $listview)
$item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
_GUICtrlListView_InsertItem($listview, "item2", 0)
GUICtrlCreateInput("", 20, 200, 150)
GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping
GUISetState()
Local $hLV = GUICtrlGetHandle( $listview )
$item1=_GUICtrlListView_GetItemParam($hLV, 0)
MsgBox( 0,"", $item1) ; <----------------------------------------
GUICtrlSetData($item2, "|ITEM1")
GUICtrlSetData($item3, "||COL33")
GUICtrlDelete($item1)
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
EndFunc ;==>Example

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

I'm wondering what is it that you want to do with this information once you get it? I understand you want to have a context menu for it, but what's going to be in the menu that is item specific? Because it may be that you're following the wrong path to get to where you want to go.

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

Link to comment
Share on other sites

FireFox, LV items created with _GUICtrlListView_InsertItem don't have an item id (ControlID).

The item id is added to native items created with GUICtrlCreateListViewItem by AutoIt and saved in the item param.

If you use _GUICtrlListView_InsertItem you can add your own item id and save in item param with _GUICtrlListView_SetItemParam.

You should start with value of 1000 or something to be sure not to interfere with the controls in your GUI.

Lars.

Link to comment
Share on other sites

@BrewManNH

Each item is linked to a unique ini key, so you will understand that each contextmenu is also unique.

I don't want to create buttons, this would take some area on my GUI.

@LarsJ

Thanks, following what you said I have tried :

_GUICtrlListView_InsertItem($hMYLW, "", 0, -1, 1000 + $i)

;OR

_GUICtrlListView_InsertItem($hMYLW, "", 0)
_GUICtrlListView_SetItemParam($hMYLW, 0, 1000 + $i)

;then
GUICtrlCreateContextMenu(1000 + $i)

But the contextmenu creation fails.

Br, FireFox.

Link to comment
Share on other sites

All I can say is post the script you're using, with a sample of the INI file, and see what we can come up with. Because it's going to be hard to do it from scratch only to find out later you're doing it a different way.

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

Link to comment
Share on other sites

All I can say is post the script you're using, with a sample of the INI file, and see what we can come up with. Because it's going to be hard to do it from scratch only to find out later you're doing it a different way.

It's not going to help us with the problem I have, but it would be better to solve it.

I know how to work around : create an item and swap it's content with the item I want.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

If your script won't help us find a work around for the question, then I guess there's no point in following up on this.

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

Link to comment
Share on other sites

If your script won't help us find a work around for the question, then I guess there's no point in following up on this.

All you (and others) need to solve the problem is the snippet I gave in the post #8, because the issue is only on the InsertItem function and the purpose is to get the item ID. Edited by FireFox
Link to comment
Share on other sites

If by Item ID you mean the control ID of the item or a handle for it, then that function doesn't return either. So, you can't assign a context menu to an item created that way, because the context menu requires a control ID and you don't have one. The function uses a sendmsg command to send a LVM_INSERTITEM message to the listview which tells it to insert your new item, the return from this message is the index number and not a discreet handle.

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

Link to comment
Share on other sites

If by Item ID you mean the control ID of the item or a handle for it, then that function doesn't return either. So, you can't assign a context menu to an item created that way, because the context menu requires a control ID and you don't have one. The function uses a sendmsg command to send a LVM_INSERTITEM message to the listview which tells it to insert your new item, the return from this message is the index number and not a discreet handle.

Yes...
Link to comment
Share on other sites

FireFox, Comments to post #7 and #8. Setting the item param just creates an item id in the sense that it uniquely identifies the rows in the listview. It does not turn the _GUICtrlListView_InsertItem created item into a proper AutoIt control. Far from. So the GUICtrlCreateContextMenu command will fail as it only works with proper AutoIt controls.

I think it's impossible to change a _GUICtrlListView_InsertItem created LV item into a proper AutoIt control, so I don't think there are any solutions to the code snippet in post #8. There are only workarounds.

Lars.

Edited by LarsJ
Link to comment
Share on other sites

@Lars

That's what I've understood after all.

Thanks for helping anyway.

My final work around will be to create a new item, move all items down, replace the first item content to what I wanted to insert.

Link to comment
Share on other sites

If you put all the LV items into an array and add them to the LV one at a time, you could do it that way. By inserting items into the array instead of the LV and then rebuilding the listview with the new array this would give you the new items where you wanted them, plus you can use GUICtrlCreateListViewItem to get a control ID so you can link your context menu to it. It would take more work, due to having to make sure the correct context menu items go to the correct LV items, but it's one way of doing it.

Or you could use a Windows message to tell you when you're right clicking the LV and read the contents of the item highlighted at that time, and customize a context menu on the fly depending upon what you have highlited.

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

Link to comment
Share on other sites

If you put all the LV items into an array and add them to the LV one at a time, you could do it that way. By inserting items into the array instead of the LV and then rebuilding the listview with the new array this would give you the new items where you wanted them, plus you can use GUICtrlCreateListViewItem to get a control ID so you can link your context menu to it. It would take more work, due to having to make sure the correct context menu items go to the correct LV items, but it's one way of doing it.

I have an array containing the listview, I did not explain myself well and I meant what you said.

Or you could use a Windows message to tell you when you're right clicking the LV and read the contents of the item highlighted at that time, and customize a context menu on the fly depending upon what you have highlited.

This would be a great idea. I will take a look about it, thanks !
Link to comment
Share on other sites

I agree with BrewManNH in the solution in the last 2 lines of post #17. Filling a LV can be time consuming. Creating context menus and menu items for every row will take even more time. But creating a context menu on the fly takes no time.

Lars.

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

; Create GUI
$hGUI = GUICreate("ListView Add Item", 400, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$cListView = GUICtrlCreateListView("", 2, 2, 394, 268)
$hListView = GUICtrlGetHandle($cListView)

; Load images
$hImage = _GUIImageList_Create()
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16))
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16))
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x0000FF, 16, 16))
_GUICtrlListView_SetImageList($hListView, $hImage, 1)

; Add columns
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 100)
_GUICtrlListView_InsertColumn($hListView, 1, "Column 2", 100)
_GUICtrlListView_InsertColumn($hListView, 2, "Column 3", 100)

; Add items
_GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0)
_GUICtrlListView_AddItem($hListView, "Row 2: Col 1", 1)
_GUICtrlListView_AddItem($hListView, "Row 3: Col 1", 2)

$c_Context_Menu_Dummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, "_Context_Menu_Dummy_Event")

GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")

GUISetState()

; Loop until user exits
While 1
    Sleep(10)
WEnd


Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
    If $iwParam = $hListView Then
        Local $aHit = _GUICtrlListView_HitTest($hListView)
        If $aHit[0] <> -1 Then

            Local $hMenu = _GUICtrlMenu_CreatePopup()
            _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", 1)
            _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", 2)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
            _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", 3)
            Local $nID = _GUICtrlMenu_TrackPopupMenu($hMenu, $iwParam, -1, -1, 1, 1, 3)
            If $nID Then GUICtrlSendToDummy($c_Context_Menu_Dummy, $aHit[0] & "|" & $nID)
            _GUICtrlMenu_DestroyMenu($hMenu)
            Return True

        EndIf
    EndIf
    ;Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_CONTEXTMENU

Func _Context_Menu_Dummy_Event()
    Local $aValue = StringSplit(GUICtrlRead($c_Context_Menu_Dummy), "|")
    MsgBox(0, "", "Item " & $aValue[1] & @CRLF & "Command " & $aValue[2])
EndFunc   ;==>_Context_Menu_Dummy_Event

Func _Exit()
    Exit
EndFunc   ;==>_Exit

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

×
×
  • Create New...