I'm trying to retrieve data from a SysListView32 object with the code below but it's returning an empty string.
The elements that I need to retrieve are those highlighted in red, as well as the others contained in the other ControlType.ListItem elements, according to the Inspector's print.
Can some one check what's wrong with my code?
Msgbox("Position the mouse cursor on the screen and press ENTER.")
Dim pt As POINTAPI
GetCursorPos(pt)
Dim hwnd As IntPtr = WindowFromPoint(pt)
Dim hwnd As IntPtr = 67202
Dim el As AutomationElement = AutomationElement.FromHandle(hwnd)
Dim walker As TreeWalker = TreeWalker.ContentViewWalker
Dim i As Integer = 0
Dim child As AutomationElement = walker.GetFirstChild(el)
While child IsNot Nothing
'
Dim k As Integer = 0
Dim child2 As AutomationElement = walker.GetFirstChild(child)
While child2 IsNot Nothing
Console.WriteLine(child2.Current.ToString)
child2 = walker.GetNextSibling(child2)
End While
child = walker.GetNextSibling(child)
End While

The SysListView32 may not provide the information requested if its current view state is not
LV_VIEW_DETAILS, so we should temporarily (if the current view state is different), use the MultipleViewPattern of its AutomationElement to verify the view state and change it if necessary using the MultipleViewPattern.SetCurrentView() method.The
SetCurrentView()method uses the same values of the Win32 Control.Then use the AutomationElement FindAll() method of the to find all child elements of type ControlType.DataItem or
ControlType.ListItem(using an OrCondition).For each of them, get all child items of type
ControlType.EditandControlType.Text(using anotherOrCondition).The position of each Item in the list is retrieved using the Item's GridItemPattern, to access the Item's Row property.
Finally, we restore the previous View State if we had to change it.
The code in the example fills a
Dictionary(Of Integer, ListViewItem)(namedsysListViewItemshere), containing all the Items extracted from the SysListView32.If you don't need ListViewItem objects, you can just store the
List(Of String), represented by theitemsTextobject, instead of creating a ListViewItem here:The Handle of a SysListView32 can also be acquired enumerating the children of its Top Level Window by
ClassName.AutomationElement.RootElement provides the current Desktop Element:
If more than one SysListView32 is found, filter by Header content, direct parent
ControlTypeorClassNameor anything else that allows to single it out.UI Automation requires a reference to the
UIAutomationClientandUIAutomationTypesassemblies.