stillife Posted January 16, 2010 Share Posted January 16, 2010 Hi, I'm brand new in this, I have an application (not mine) that have a panel with listbox and I need to select a couple of the items (highlight them). I already know the index (row) of the item, I'm just having difficulty finding a function that will select an item based on the index. I have looked at: _GUICtrlListBox_SetCurSel and ControlCommand I've inspected the window with the Window Info tool and found out that the control is a ListBox: >>>> Control <<<< Class: ListBox Instance: 1 ClassnameNN: ListBox1 Name: Advanced (Class): [CLASS:ListBox; INSTANCE:1] ID: 1014 Text: Position: 5, 5 Size: 386, 644 ControlClick Coords: 217, 267 Style: 0x50210901 ExStyle: 0x00000204 Handle: 0x00410BD6 I'm interested in selecting individual items as well as if possible a range of items. Any help is greately appreciated Thanks. Link to comment Share on other sites More sharing options...
dantay9 Posted January 16, 2010 Share Posted January 16, 2010 (edited) ControlCommand works on an autoit gui. I don't see why it wouldn't work on another program's listbox. Here is my test: #include <GuiListBox.au3> $GUI = GUICreate("", 100, 100) ;create gui $ListBox = _GUICtrlListBox_Create($GUI, "Entry1", 0, 0) ;create listbox For $i = 2 To 5 ;for loop _GUICtrlListBox_AddString($ListBox, "Entry" & $i) ;add some entries Next GUISetState() ;show the gui Sleep(2000) ;sleep 2 seconds ControlCommand($GUI, "", $ListBox, "SetCurrentSelection", 2) ;select the third item Sleep(4000) ;sleep 4 seconds As far as selecting multiple items, you will have to get the handle of the list box and use that in the _GUICtrlListBox_ functions. I think _GUICtrlListBox_SelItemRange() is what you are looking for. Edited January 16, 2010 by dantay9 Link to comment Share on other sites More sharing options...
stillife Posted January 16, 2010 Author Share Posted January 16, 2010 Thank you for your prompt reply. Well I'm not sure what my problem is. Here is what I've tried so far: Dim $ttlWindow Dim $hdlWindow Dim $cidList Dim $hdlList $ttlWindow = "Select Active Members" $cidList = "[CLASS:ListBox; INSTANCE:1]" $hdlWindow = WinGetHandle($ttlWindow) $hdlList = ControlGetHandle($hdlWindow, "", $cidList) ControlCommand($hdlWindow, "", $hdlList, "SetCurrentSelection", 6) I've been able to confirm that the handles that I'm storing are the right ones (I output them and check with the Window Info tool). However, it seems that my ListBox doesn't this. Anything I'm missing here. Regards. Link to comment Share on other sites More sharing options...
dantay9 Posted January 16, 2010 Share Posted January 16, 2010 Looks fine to me. The code you are using will select the 7th item in the list view. Not sure what is going on exactly. Link to comment Share on other sites More sharing options...
stillife Posted January 16, 2010 Author Share Posted January 16, 2010 I'm not sure what could be the problem. Do I need any special includes for this to work? I just started with AutoIt this morning so I'm pretty sure I'm doing something very stupid. Any other ideas? Link to comment Share on other sites More sharing options...
dantay9 Posted January 16, 2010 Share Posted January 16, 2010 I'm out of ideas right at the moment. My only suggestion would be to wait 24 hours until some of the more experienced coders get out of bed. Link to comment Share on other sites More sharing options...
stillife Posted January 16, 2010 Author Share Posted January 16, 2010 An interesting update. I thought I would see if there is in fact anything being selected so I did this: ControlCommand($hdlWindow, "", $hdlList, "SetCurrentSelection", 6) $myitem = ControlCommand($hdlWindow, "", $hdlList, "GetCurrentSelection") MsgBox(0,"Test", $myitem) I found out that it has somehow selected the item (because it reads the text from the item), but doesn't seem to highlight it. I don't really care about highlighting but the problem is that once I select the item I want to click on a button which will perform a task on the list item: ControlClick($hdlWindow, "", $cidBtnActive) However when I click the button, nothing happens. It is as if unless the item is highlighted it is not officially selected. This is really interesting. I thought I should mention this. Regards Link to comment Share on other sites More sharing options...
stillife Posted January 16, 2010 Author Share Posted January 16, 2010 Ok, I think I figured out what was wrong. The ControlCommand function (with SetCurrentSelection) only selects an item in the listbox without actually selecting it (highlighting it). In order to highlight the item, it seems that you have to use the _GUICtrlListBox_ClickItem() function. In case someone else is wondering how this is done: #include <GuiListBox.au3> Dim $ttlWindow Dim $hdlWindow Dim $cidList Dim $hdlList $ttlWindow = "Select Active Members" $cidList = "[CLASS:ListBox; INSTANCE:1]" $hdlWindow = WinGetHandle($ttlWindow) $hdlList = ControlGetHandle($hdlWindow, "", $cidList) _GUICtrlListBox_ClickItem($hdlList, 4, "left") The above code selects tht 5th item on the list and actually highlights it. You can also use ControlClick following this to perform a task on the selected item. If someone has a better method or better explanation, I would really appreciate it if they share it with me and everyone else. Thank you all, especially dantay9 for your help Regards Link to comment Share on other sites More sharing options...
stillife Posted January 16, 2010 Author Share Posted January 16, 2010 Hi all, Now I've got another strange problem. I'm trying to select multiple ranges in my listbox and click on a button. The first range works fine and the items are first selected and the button is pushed and the task is done. The second time, the script just doesn't work. Here is what I've got: #include <GuiListBox.au3> Dim $ttlWindow Dim $hdlWindow Dim $cidList Dim $hdlList Dim $cidBtnActive Dim $intMemRange Dim $arrMemRange Dim $arrMembers[3] ; Just as a sample to test the script $arrMembers[0] = "33" $arrMembers[1] = "4-23" ;first range of items I need selected $arrMembers[2] = "34-36" ;second range of items I need selected $ttlWindow = "Select Active Members" $cidList = "[CLASS:ListBox; INSTANCE:1]" $cidBtnActive = "[CLASS:Button; INSTANCE:3]" $hdlWindow = WinGetHandle($ttlWindow) $hdlList = ControlGetHandle($hdlWindow, "", $cidList) For $intMemRange = 1 to UBound($arrMembers) - 1 $arrMemRange = StringSplit($arrMembers[$intMemRange], "-") If $arrMemRange[0] = 2 Then _GUICtrlListBox_ClickItem($hdlList, Int($arrMemRange[1]) - 1, "left") Send("{SHIFTDOWN}") ;Hold down shift _GUICtrlListBox_ClickItem($hdlList, Int($arrMemRange[2]) - 1, "left") Send("{SHIFTUP}") ;Releases shift Else _GUICtrlListBox_ClickItem($hdlList, $arrMemRange[1] - 1, "left") EndIf ControlClick($hdlWindow, "", $cidBtnActive) Next I thought that the above loop would allow me to first select items 4-23, click the activate button, then select items 34-36 and then click the activate button. However only the first range is selected and activated. When $intMemRange is incremented, the script successfully splits the range into 34 and 36, but when I get to line _GUICtrlListBox_ClickItem, nothing is selected and stragely enough another one of my open windows pops open (my browser for example). Again I'm new at this so it is probably something very stupid. Any help is greatly appreciated. Regards Link to comment Share on other sites More sharing options...
stillife Posted January 17, 2010 Author Share Posted January 17, 2010 Just an update. I still have the same problem. I thought maybe the Send("{SHIFTDOWN}") and Send("{SHIFTUP}") procedure might be the cause, but nothing I've done seems to indicate exacatly what is the issue. So if anyone has any thoughts I would appreciate it if they let me know. Link to comment Share on other sites More sharing options...
dantay9 Posted January 17, 2010 Share Posted January 17, 2010 Have you tried _GUICtrlListBox_SelItemRange? Or _GUICtrlListBox_SetSel? Link to comment Share on other sites More sharing options...
stillife Posted January 17, 2010 Author Share Posted January 17, 2010 Have you tried _GUICtrlListBox_SelItemRange? Or _GUICtrlListBox_SetSel? Yes I've tried those, but what I found out was that those two methods only seem to select the items in the background (or in memory or something like that). They don't actually highlight the actual items and that means I can't click on a button once the the items are selected to perform the required task on them. Here are a couple of screenshots to make things a bit more clear. I have a main application window. I'm using the menu to select the item highlighted in the picture: This opens up a second window. Here I need to select one item, a range of items or many ranges of items in the list box and then click the "Activate" button. One thing to note is that once you click activate the text of the listbox items are updated: This is essentially what I want to do. I hope this helps make my problem more clear. Thank you for your help. Link to comment Share on other sites More sharing options...
stillife Posted January 17, 2010 Author Share Posted January 17, 2010 I'm really sorry about all the replies. But I'm hoping by providing updates, I may be able to help the experts find the problem. My newest test confirms that _GUICtrlListBox_ClickItem() function fails to select items in the listbox which are not visible on screen. That is to say if you need to scroll down the list to see the item, that item won't be selected. For example in the picture I posted in my previous post I can successfuly select items up to 49, but I won't be able to select any items futher down. So I'm wondering if there is any workaround this issue? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now