Sign in to follow this  
Followers 0
Jefferds44

How to select and give focus to a row [SOLVED]

17 posts in this topic

#1 ·  Posted (edited)

Hi guys,

I'm trying to select a specific row in a listbox and then execute it. I can execute it by sending an "enter" command. But for some reason I can't select and give focus to it prior to executing the {enter}.

I am able to browse through each line using the ControlListView function and the "GetText" command. But the "select" command doesn't seem to fully perform what I expect it to.

I am using this function/command as follows:

ControlListView ($WindowTitle, "", 46, "Select", $line);

However, when I follow this up with an {enter}, it always opens the first line. The "Select" command seems to highlight the right row, but it doesn't seem to get focus. Is there another way to give focus to a specific row in a list box?

Thanks

Edited by Jefferds44

Share this post


Link to post
Share on other sites



Is there another way to give focus to a specific row in a list box?

_GUICtrlListBox_SetCurSel() ?

Share this post


Link to post
Share on other sites

I think I'm not doing something right. I tried the listbox function but I think that I should be using the _GuiCtrlListView functions because my control is of type CLASS:SysListView32.

Could someone please explain to me how the $index variable works? Is this supposed to be the row number? Also, do the optional parameters, $fSelected and $fFocused actually select and give focus to the appropriate line?

_GUICtrlListView_SetItemSelected

--------------------------------------------------------------------------------

Sets whether the item is selected

#Include <GuiListView.au3>

_GUICtrlListView_SetItemSelected($hWnd, $iIndex[, $fSelected = True[, $fFocused = False]])

Parameters

$hWnd Handle to the control

$iIndex Zero based index of the item, -1 to set selected state of all items

$fSelected If True the item(s) are selected, otherwise not.

$fFocused If True the item has focus, otherwise not.

Share this post


Link to post
Share on other sites

After experimenting further with _GUICtrlListView_SetItemSelected, it crashes the program that it interfaces with. Does anyone have an idea how Autoit could be crashing the target application?

Thanks,

Jeff

Share this post


Link to post
Share on other sites

Because it's external SysListView I believe and the _Gui* library is accepting handles that are just fine because the behavior is interprocess but if you want to send structs or pointer to strings to this function and preform send message to other process you'll probably send valid message with null pointers and this might be the case, (in most trivial tasks it wouldn't however), it's also depend, what do you want this function to do?

Share this post


Link to post
Share on other sites

what do you want this function to do?

Well I'm creating a script that will automatically open poker tables for me from a list of tables in a poker lobby room. I want to open tables based on selected criteria.

Basically, what I want from this function is for it to select and give focus to a specific line in the list so that I can open the right table.

Does this help explain what I want to do?

Share this post


Link to post
Share on other sites

Well, I might complicate things as I usually do '_- but I think with a little effort from your side you might get around this, bear with me please...

First, I really don't know what notification or list view message you should use but the concept is quite global and in most cases it involves LV_ITEM struct sending as a parameter.

So, what you should try to do if you got valid handle for the list view is to initialize LV_ITEM or LV_FINDINFO struct and allocate enough memory on the address space of the other process. First using VirtualAllocEx you'll successfully receive the address and for the actual memory copy use the function WriteProcessMemory (you'll need the process's thread handle - use OpenProcess to get this one).

next send a message to find the item with the specified string in the struct you've allocated in the callee process's address space and from there, just play around with messages and allocations as necessary. Sorry, I really can't think of an actual example but the concept is valid an not just in theory :) . Good-luck

Share this post


Link to post
Share on other sites

You had me at "hello". :lmao:

Haha, thanks for the support buddy. I'll try to work on what you suggest.

Well, I might complicate things as I usually do '_- but I think with a little effort from your side you might get around this, bear with me please...

First, I really don't know what notification or list view message you should use but the concept is quite global and in most cases it involves LV_ITEM struct sending as a parameter.

So, what you should try to do if you got valid handle for the list view is to initialize LV_ITEM or LV_FINDINFO struct and allocate enough memory on the address space of the other process. First using VirtualAllocEx you'll successfully receive the address and for the actual memory copy use the function WriteProcessMemory (you'll need the process's thread handle - use OpenProcess to get this one).

next send a message to find the item with the specified string in the struct you've allocated in the callee process's address space and from there, just play around with messages and allocations as necessary. Sorry, I really can't think of an actual example but the concept is valid an not just in theory :) . Good-luck

Share this post


Link to post
Share on other sites

Could someone please explain to me how the $index variable works? Is this supposed to be the row number? Also, do the optional parameters, $fSelected and $fFocused actually select and give focus to the appropriate line?

Any info on the above quote please?

Share this post


Link to post
Share on other sites

Read the function definition to understand how it achieves this intention because I suspect it involves sendmessage or so with pointer to LV_DISPINFO which is not valid for in the content of the other process's address space. Just a guess though.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Ok so I spent the last hour on trying to work on your recommendations and it's too high above my skill level. I have no experience working with memory.

I seem so close to my objective of being able to give focus to a specific row in a listview...

I tried using the "ControlListView" function with the "select" command. It actually highlights the right line, but doesn't put the rectangle indicating "focus".

Then I tried the "_GUICtrlListView_SetItemSelected" UDF to select AND give focus to the right line. But when I use it, it crashes the target application.

So, I'm sorry to come full cycle on this but here are two questions again.

A ) Can I use any of the main Autoit functions like "ControlListView" to give focus to a specific line in a SysListView32 control?

B ) If the answer to A is "no", then how can I make sure that using "_GUICtrlListView_SetItemSelected" doesn't crash the target application?

Sorry if I seem slow here, but this problem has been kicking my ass for the past 2 days.

Thanks,

Jeff

Edited by Jefferds44

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Is the hWnd you're passing to the function is of the parent window or of the listview window? because I'm reading the function definition right now and it actually does use VirtualAllocEx but only if the macro @OSTYPE is not equal to "WIN32_WINDOWS" and it might involve more investigating why it uses this function and not directly VirtualAllocEx instead but it's just me and I might be wrong. You should be able to successfully call this function if hWnd = SysListView32 handle and the allocation finished successfully because otherwise this is the problem.

Edit: OK, I see that unless you got win98 and below @OSTYPE is the correct method to validate if it's possible to call the VirtualAllocEx function so it's not the problem so... first confirm that it's not hWnd pointing to the parent window instead ^^

Edited by Authenticity

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>






   
GUICreate("ListView Add Column", 400, 330)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268,BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT))
$Button1 = GUICtrlCreateButton("SetSelected Row 2", 10, 275,193, 25, 0)
$Button2 = GUICtrlCreateButton("SetSelected All", 10, 300, 193, 25, 0)
$Button3 = GUICtrlCreateButton("GetSelected", 220, 275,170, 25, 0)
$Button4 = GUICtrlCreateButton("Clear Selected", 220, 300,170, 25, 0)
    
   
    GUISetState()

  
  
   
    _GUICtrlListView_AddColumn($hListView, "Column 1", 100)
    _GUICtrlListView_AddColumn($hListView, "Column 2", 100)
    _GUICtrlListView_AddColumn($hListView, "Column 3", 100)

    
    _GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0)
    $Row_2 = _GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 3", 2, 2)
    _GUICtrlListView_AddItem($hListView, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem($hListView, 1, "Row 2: Col 2", 1, 2)
    _GUICtrlListView_AddItem($hListView, "Row 3: Col 1", 2)

_GUICtrlListView_SetExtendedListViewStyle ($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))


Do
$msg = GUIGetMsg()
Switch($msg)
Case $Button1
_GUICtrlListView_SetItemSelected($hListView, $Row_2,True,True)
$Indices =_GUICtrlListView_GetSelectedIndices($hListView)
$Indices = StringSplit($Indices, "|")
For $IX = 1 To $Indices[0] Step 1 
$TXTITEM =_GUICtrlListView_GetItemText($hListView, int($Indices[$IX]))
 MsgBox(4160, "Information", "Item Selected: " & $TXTITEM)
Next
Case $Button2
_GUICtrlListView_SetItemSelected($hListView, -1,True,True)
Case $Button3
$Indices =_GUICtrlListView_GetSelectedIndices($hListView)
$Indices = StringSplit($Indices, "|")
For $IX = 1 To $Indices[0] Step 1 
$TXTITEM =_GUICtrlListView_GetItemText($hListView, int($Indices[$IX]))
 MsgBox(4160, "Information", "Item Selected: " & $TXTITEM)
Next
Case $Button4
_GUICtrlListView_SetItemSelected($hListView, -1,False,False)
EndSwitch
Until $msg = $GUI_EVENT_CLOSE
Exit

Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

or

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>





   
GUICreate("ListView Add Column", 400, 330)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268,BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT))
$Button1 = GUICtrlCreateButton("SetSelected Row 2", 10, 275,193, 25, 0)
$Button2 = GUICtrlCreateButton("SetSelected All", 10, 300, 193, 25, 0)
$Button3 = GUICtrlCreateButton("GetSelected", 220, 275,170, 25, 0)
$Button4 = GUICtrlCreateButton("Clear Selected", 220, 300,170, 25, 0)
 
   
    GUISetState()

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

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

_GUICtrlListView_SetExtendedListViewStyle ($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))


Do
$msg = GUIGetMsg()
Switch($msg)
Case $Button1
$title = WinGetTitle("", "")
$var = ControlGetText($title,"",$hListView)
ControlFocus ($title,$var,$hListView)
ControlListView ( $title, $var, $hListView, "Select" , $Row_2 , $Row_2 )

Case $Button2
$title = WinGetTitle("", "")
$var = ControlGetText($title,"",$hListView)
ControlFocus ($title,$var,$hListView)
ControlListView ( $title, $var, $hListView, "SelectAll")
Case $Button3
$title = WinGetTitle("", "")
$var = ControlGetText($title,"",$hListView)
ControlFocus ($title,$var,$hListView)
$GetSelected = ControlListView ( $title, $var, $hListView, "GetSelected" , 1)
$GetSelected = StringSplit($GetSelected, "|")
For $IX = 1 To $GetSelected[0] Step 1 
$TXTITEM =ControlListView ( $title, $var, $hListView, "GetText" ,$GetSelected[$IX])
 MsgBox(4160, "Information", "Item Selected: " & $TXTITEM)
Next
Case $Button4
$title = WinGetTitle("", "")
$var = ControlGetText($title,"",$hListView)
ControlFocus ($title,$var,$hListView)
$GetSelected = ControlListView ( $title, $var, $hListView, "GetSelected" , 1)
$GetSelected = StringSplit($GetSelected, "|")
ControlListView ( $title, $var, $hListView, "DeSelect",0,$GetSelected[0])
EndSwitch
Until $msg = $GUI_EVENT_CLOSE
Exit

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

These 2 code examples only "select" the row. They don't give it focus (a.k.a. put the focus rectangle around the row). I can select the different rows just fine, it's the focus part I'm having a hard time with.

Share this post


Link to post
Share on other sites

Ok, so I solved my problem. I'm ashamed to admit but I hadn't updated Autoit for a while. I decided to install the latest version and it solved my problem.

Thanks to everyone for helping me solve this.

Share this post


Link to post
Share on other sites

These 2 code examples only "select" the row. They don't give it focus (a.k.a. put the focus rectangle around the row). I can select the different rows just fine, it's the focus part I'm having a hard time with.

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#Include <WinAPI.au3>

#include <TabConstants.au3>





   
$hwnd = GUICreate("ListView Add Column", 400, 360)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268,BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT))
$Button1 = GUICtrlCreateButton("SetSelected Row 2", 10, 275,193, 25, 0)
$Button2 = GUICtrlCreateButton("SetSelected All", 10, 300, 193, 25, 0)
$Button3 = GUICtrlCreateButton("GetSelected", 220, 275,170, 25, 0)
$Button4 = GUICtrlCreateButton("Clear Selected", 220, 300,170, 25, 0)
$Button5 = GUICtrlCreateButton("Get_Focus", 10, 332,170, 25)
$Button6 = GUICtrlCreateButton("Set_Focus_Row 2", 220, 332,170, 25)

   
    GUISetState()

  
  
   
    _GUICtrlListView_AddColumn($hListView, "Column 1", 100)
    _GUICtrlListView_AddColumn($hListView, "Column 2", 100)
    _GUICtrlListView_AddColumn($hListView, "Column 3", 100)

    
    _GUICtrlListView_AddItem($hListView, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($hListView, 0, "Row 1: Col 3", 2, 2)
    _GUICtrlListView_AddItem($hListView, "Row 2: Col 1", 1)
    $Row_2 = _GUICtrlListView_AddSubItem($hListView, 1, "Row 2: Col 2", 1, 2)
    _GUICtrlListView_AddItem($hListView, "Row 3: Col 1", 2)

_GUICtrlListView_SetExtendedListViewStyle ($hListView, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES))


Do
$msg = GUIGetMsg()
Switch($msg)
Case $Button1
_GUICtrlListView_SetItemSelected($hListView, $Row_2,True,True)
$Indices =_GUICtrlListView_GetSelectedIndices($hListView)
$Indices = StringSplit($Indices, "|")
For $IX = 1 To $Indices[0] Step 1 
$TXTITEM =_GUICtrlListView_GetItemText($hListView, int($Indices[$IX]))
 _WinAPI_SetFocus(_WinAPI_GetDlgItem($hwnd, $hListView))
 _GUICtrlListView_SetItemState($hListView,int($Indices[$IX]), $LVIS_FOCUSED, $LVIS_FOCUSED)
 MsgBox(4160, "Information", "Item Selected: " & $TXTITEM)
Next
Case $Button2
_GUICtrlListView_SetItemSelected($hListView, -1,True,True)
_WinAPI_SetFocus(_WinAPI_GetDlgItem($hwnd, $hListView))



Case $Button3
$Indices =_GUICtrlListView_GetSelectedIndices($hListView)
$Indices = StringSplit($Indices, "|")
For $IX = 1 To $Indices[0] Step 1 
$TXTITEM =_GUICtrlListView_GetItemText($hListView, int($Indices[$IX]))
_WinAPI_SetFocus(_WinAPI_GetDlgItem($hwnd, $hListView))
MsgBox(4160, "Information", "Item Selected: " & $TXTITEM)
Next
Case $Button4
_GUICtrlListView_SetItemSelected($hListView, -1,False,False)
_WinAPI_SetFocus(_WinAPI_GetDlgItem($hListView, $hListView))
Case $Button5

$Indices =_GUICtrlListView_GetSelectedIndices($hListView)
$Indices = StringSplit($Indices, "|")
For $IX = 1 To $Indices[0] Step 1 
$TXTITEM =_GUICtrlListView_GetItemText($hListView, int($Indices[$IX]))
$DlgItem = _WinAPI_SetFocus(_WinAPI_GetDlgItem($hwnd, $hListView))
$ItemFocused = _GUICtrlListView_GetItemFocused($hListView, int($Indices[$IX]))
if $ItemFocused = True Then MsgBox(4160, "Information", "Item Focused: " & $TXTITEM)
Next
Case $Button6
$DlgItem = _WinAPI_SetFocus(_WinAPI_GetDlgItem($hwnd, $hListView))
_GUICtrlListView_SetItemSelected($hListView, $Row_2,True,True)
 _GUICtrlListView_SetItemState($hListView,$Row_2, $LVIS_FOCUSED, $LVIS_FOCUSED)
EndSwitch
Until $msg = $GUI_EVENT_CLOSE
Exit

صرح السماء كان هنا

 

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