Getting selected item from ListView ??

Class library usage, coding and language questions.
vonBrabant
Posts: 15
Joined: Wed May 23, 2007 11:12 pm

Getting selected item from ListView ??

Post by vonBrabant » Thu May 22, 2008 9:52 pm

Hi,

sorry if this is trivial, but I can't find a way to get the index (or text) of the currently selected item of a list view control :-(

I can select items with ListViewSelectItemIndex but there is no corresponding function for finding out which item is selected.

What am I overlooking ?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Fri May 23, 2008 10:21 am

Unfortunately, this function is not available. :oops:

The following workaround can be used:

From your ListView, get the Element and walk through its children (which are of Role ListItem). The item which is currently selected has the state "selected", not selected items have the state "selectable"

Michael
Ranorex Team

vonBrabant
Posts: 15
Joined: Wed May 23, 2007 11:12 pm

Post by vonBrabant » Fri May 23, 2008 12:19 pm

Okay, that would be good enough, but it doesn't really work.

The problem is that STATE_SYSTEM_SELECTED evaluates to '2', while ElementGetState returns either 2097152 (2^21) if the item is not selected and 2097154 (2^21+2) if the item is selected !!
So, am I supposed to play around with bit masks for this comparison to work, or what am I doing wrong ?

Many thanks


Code: Select all

element = Ranorex.ControlGetElement(listViewHwnd)
itemCount = Ranorex.ElementFindChildren(element,Ranorex.ROLE_SYSTEM_LISTITEM,
                                            None,None,Ranorex.MATCH_EXACT)
for idx in range(itemCount):
    childE = Ranorex.ElementGetChildFieldItem(idx)
    bla = Ranorex.ElementGetState(childE)      # for debugging
    bli = Ranorex.STATE_SYSTEM_SELECTED   # for debugging
    if Ranorex.ElementGetState(childE)==Ranorex.STATE_SYSTEM_SELECTED:
            break
# for

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Fri May 23, 2008 1:03 pm

Just use

Ranorex.ElementGetState(childE) & Ranorex.STATE_SYSTEM_SELECTED
instead of
Ranorex.ElementGetState(childE)==Ranorex.STATE_SYSTEM_SELECTED

vonBrabant
Posts: 15
Joined: Wed May 23, 2007 11:12 pm

Post by vonBrabant » Fri May 23, 2008 5:26 pm

yeah, you are right.
Everything is find ;-)