Sign in to follow this  
Followers 0
Rishav

_GUICtrlListView_GetItemText does not takes variable as index

6 posts in this topic

Hi folks

I have a weird problem. Most likely I am doing something wrong but this may be a bug in Autoit too.

The problem is that when i use a number as the index in _GUICtrlListView_GetItemTextString or _GUICtrlListView_GetItemText, it works fine. But when i use a variable, it gives me an empty value.

In the below code, the variable SelectedCompare_row gives the number of the selcted index.

if i use this number in the get text functions, i get proper output. But if I use the variable itself, the output is null.

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

; Create the Gui & gui elements
$LogViewer_gui = GUICreate("System Log Viewer", 400, 300, -1, -1)

; Show GUI
GUISetState()

;Use first row as header
$LogViewer_grid = GUICtrlCreateListView("Col1|Col2|Col3", 10, 10, 380, 280, $LVS_SINGLESEL)
_GUICtrlListView_SetExtendedListViewStyle($LogViewer_grid, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_RegisterSortCallBack($LogViewer_grid)
GUICtrlSetBkColor($LogViewer_grid, $GUI_BKCOLOR_LV_ALTERNATE)

; Add listview items
_GUICtrlListView_AddItem($LogViewer_grid, "Item 11")
_GUICtrlListView_AddSubItem($LogViewer_grid, 0, "Item 12", 1)
_GUICtrlListView_AddSubItem($LogViewer_grid, 0, "Item 13", 2)

_GUICtrlListView_AddItem($LogViewer_grid, "Item 21")
_GUICtrlListView_AddSubItem($LogViewer_grid, 1, "Item 22", 1)
_GUICtrlListView_AddSubItem($LogViewer_grid, 1, "Item 23", 2)

_GUICtrlListView_AddItem($LogViewer_grid, "Item 31")
_GUICtrlListView_AddSubItem($LogViewer_grid, 2, "Item 32", 1)
_GUICtrlListView_AddSubItem($LogViewer_grid, 2, "Item 33", 2)


; Create a context menu for the grid
$LogViewer_grid_contextmenu = GUICtrlCreateContextMenu($LogViewer_grid)
$LogViewer_grid_1_contextmenuitem = GUICtrlCreateMenuItem("Show Info", $LogViewer_grid_contextmenu)


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _GUICtrlListView_UnRegisterSortCallBack($LogViewer_grid)
            ExitLoop
        Case $LogViewer_grid
            _GUICtrlListView_SortItems($LogViewer_grid, GUICtrlGetState($LogViewer_grid))

        Case $LogViewer_grid_1_contextmenuitem
            $SelectedCompare_row = _GUICtrlListView_GetSelectedIndices($LogViewer_grid)
            $SelectedCompare_file1 = _GUICtrlListView_GetItemTextString($LogViewer_grid, $SelectedCompare_row)
            MsgBox(0, $SelectedCompare_row, $SelectedCompare_file1)
            $SelectedCompare_file2 = _GUICtrlListView_GetItemTextString($LogViewer_grid, 2)
            MsgBox(0, $SelectedCompare_row, $SelectedCompare_file2)
    EndSwitch
WEnd

regards

Rishav

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

Make it an Int instead of a string..

$SelectedCompare_file1 = _GUICtrlListView_GetItemTextString($LogViewer_grid, Int($SelectedCompare_row))

Or

Since your not using multi select of items then use a function that returns an Int

$SelectedCompare_row = _GUICtrlListView_GetNextItem($LogViewer_grid)
$SelectedCompare_file1 = _GUICtrlListView_GetItemTextString($LogViewer_grid, $SelectedCompare_row)

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites

Both of you are correct. works now. Thanks a lot guys. :D

Share this post


Link to post
Share on other sites

Better way: Use always the handle instead the id in Listview-UDF

$SelectedCompare_file1 = _GUICtrlListView_GetItemTextString(GUICtrlGetHandle($LogViewer_grid), $SelectedCompare_row)

Not really as it makes no difference to the ops problem, since the function itself checks if it's a Control ID or hWnd, if it's an ID then it gets the handle..

One function that it does need the hWnd is _GUICtrlListView_HitTest()

The issue the op is having is because $SelectedCompare_row = _GUICtrlListView_GetSelectedIndices($LogViewer_grid) is returning a String not an Int.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Smashly, he is also right. If i use the handle instead of the id, it works. :D

rather confusing.

in below code, i have just used the controlget handle. otherwise the code is identical to the on i posted in op.

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

; Create the Gui & gui elements
$LogViewer_gui = GUICreate("System Log Viewer", 400, 300, -1, -1)

; Show GUI
GUISetState()

;Use first row as header
$LogViewer_grid = GUICtrlCreateListView("Col1|Col2|Col3", 10, 10, 380, 280, $LVS_SINGLESEL)
_GUICtrlListView_SetExtendedListViewStyle($LogViewer_grid, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_RegisterSortCallBack($LogViewer_grid)
GUICtrlSetBkColor($LogViewer_grid, $GUI_BKCOLOR_LV_ALTERNATE)

; Add listview items
_GUICtrlListView_AddItem($LogViewer_grid, "Item 11")
_GUICtrlListView_AddSubItem($LogViewer_grid, 0, "Item 12", 1)
_GUICtrlListView_AddSubItem($LogViewer_grid, 0, "Item 13", 2)

_GUICtrlListView_AddItem($LogViewer_grid, "Item 21")
_GUICtrlListView_AddSubItem($LogViewer_grid, 1, "Item 22", 1)
_GUICtrlListView_AddSubItem($LogViewer_grid, 1, "Item 23", 2)

_GUICtrlListView_AddItem($LogViewer_grid, "Item 31")
_GUICtrlListView_AddSubItem($LogViewer_grid, 2, "Item 32", 1)
_GUICtrlListView_AddSubItem($LogViewer_grid, 2, "Item 33", 2)


; Create a context menu for the grid
$LogViewer_grid_contextmenu = GUICtrlCreateContextMenu($LogViewer_grid)
$LogViewer_grid_1_contextmenuitem = GUICtrlCreateMenuItem("Show Info", $LogViewer_grid_contextmenu)


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _GUICtrlListView_UnRegisterSortCallBack($LogViewer_grid)
            ExitLoop
        Case $LogViewer_grid
            _GUICtrlListView_SortItems($LogViewer_grid, GUICtrlGetState($LogViewer_grid))

        Case $LogViewer_grid_1_contextmenuitem
            $SelectedCompare_row = _GUICtrlListView_GetSelectedIndices($LogViewer_grid)
            $SelectedCompare_file1 = _GUICtrlListView_GetItemTextString(ControlGetHandle("","",$LogViewer_grid), $SelectedCompare_row)
            MsgBox(0, $SelectedCompare_row, $SelectedCompare_file1)
            $SelectedCompare_file2 = _GUICtrlListView_GetItemTextString(ControlGetHandle("","",$LogViewer_grid), 2)
            MsgBox(0, $SelectedCompare_row, $SelectedCompare_file2)
    EndSwitch
WEnd
Edited by Rishav

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