TListView set selected item lazarus / 31-LCL ---------------------------------------------------------------------- felipemdc · 2012-03-29 14:17 Hello, Is anyone having success in selecting the item of a TListView? In my code I clear it and add some items and then try to set the selected item with:   listCountries.ItemIndex := 0; or   listCountries.Selected := listCountries.Items[0]; But neither works =/ ---------------------------------------------------------------------- Blaazen · 2012-03-29 14:53 Both works here on Qt4: Lazarus 0.9.31 r35805M FPC 2.5.1 x86_64-linux- qt I didn't test elsewhere. ---------------------------------------------------------------------- felipemdc · 2012-03-29 15:29 My test was in Win32 ---------------------------------------------------------------------- Blaazen · 2012-03-29 15:42 It works on GTK2-appearance as well. ---------------------------------------------------------------------- Vingadero · 2012-03-29 17:21 Working here on Win64 too... ---------------------------------------------------------------------- Pascaluvr · 2012-03-29 17:33 Do you mean that the item is not highlighted as selected?  Make sure you do SetFocus for the listview. ---------------------------------------------------------------------- jwdietrich · 2012-03-30 22:04 It is working without problem on the Carbon widgetset on Mac OS X. The first line listCountries.ItemIndex := 0; works as is, the second line has to be formulated as listCountries.Selected[0] := true; ---------------------------------------------------------------------- PeterX · 2016-02-10 11:49 >>> Is anyone having success in selecting the item of a TListView? I currently also work on SELECTING AN ITEM of a TListView. - Lazarus 1.4.0 - Windows 7 64 Bit But both types of call such as .. Code: [Select]listCountries.ItemIndex := i;Code: [Select]listCountries.Selected := listCountries.Items[i]; do not work for me. ... ---------------------------------------------------------------------- wp · 2016-02-10 12:40 Quote from: PeterX on February 10, 2016, 12:49:17 pmBut both types of call such as .. - listCountries.ItemIndex := i; - listCountries.Selected := listCountries.Items[ i]; do not work for me. ...  Hey - no need to get angry! Both calls depend on the value of i which  must be >= 0 and < listCountries.Count. ---------------------------------------------------------------------- taazz · 2016-02-10 12:41 Quote from: PeterX on February 10, 2016, 12:49:17 pm>>> Is anyone having success in selecting the item of a TListView? I currently also work on SELECTING AN ITEM of a TListView. - Lazarus 1.4.0 - Windows 7 64 Bit But both types of call such as .. - listCountries.ItemIndex := i; - listCountries.Selected := listCountries.Items; do not work for me. ...  works for me Code: [Select]  ListBox1.ItemIndex := SpinEdit2.Value; or [code]   ListBox1.Selected[SpinEdit1.Value] := True; [/code]if you have multiselect set to true this one can be used to select multiple items. ---------------------------------------------------------------------- PeterX · 2016-02-10 22:54 Hm ...   ??!??!??!??!??!??!? The combination of       ListView.ItemIndex:= i;       ListView.Selected; did the job now. BUT WHY ??????????????????????? For me this does NOT make any sense. Can this be found in the documentation this way ? Does this also work under DELPHI this curious way ? Thanks, Peter ---------------------------------------------------------------------- wp · 2016-02-10 23:16 Quote from: PeterX on February 10, 2016, 11:54:26 pmThe combination of       ListView.ItemIndex:= i;       ListView.Selected; did the job now. This is certainly not correct, one of them is enough - see attached demo. ---------------------------------------------------------------------- PeterX · 2016-02-11 13:18 Sorry, I'm on the job and can't test the demo code. On (only)    ListView.ItemIndex:= i; nothing happens in my app .. :-( I will try this evening, probably there's some buggy things to find in my code ;-) ---------------------------------------------------------------------- balazsszekely · 2016-02-11 13:21 Quote@PeterX nothing happens in my app .. :-(Because ListView is not focused, try to focus ListView with the SetFocus method. Alternatively you can set HideSelection property to False. ---------------------------------------------------------------------- PeterX · 2016-02-11 15:25 From the old Delphi version of my program the .SetFocus() method call was already there. But didn't work, doesn't work, I'm sorry. ---------------------------------------------------------------------- wp · 2016-02-11 15:46 Tried above demo with my old Laz 1.4.0 (32-bit) installation on Win-7 64bit. Running fine (i.e. only one of both methods needed). Is your Lazarus the 32-bit or 64-bit version? If 64-bit install the 32-bit as a secondary installation, 64-bit does not have a real advantage for most programs, but faces you with several disadvantages (debugger). ---------------------------------------------------------------------- balazsszekely · 2016-02-11 16:33 @wp Your test project works fine. @PeterX Please attach a simple application that demonstrates the issue, otherwise we just shot in the dark. ---------------------------------------------------------------------- PeterX · 2016-02-11 21:19 Hello GetMem, I think about extracting the whole code of the Tree+ListView as a separate component - to make it usable in more than this current application. I will start working on this tomorrow. If the mysterious behaviore still occurs there, I'll post again. But read back the whole thread. Other people also said that it didn't work for them .. and this is years ago, it's a really old thread. But it's true, without example code You will not be able to reproduce this behaviore :-( => I will start working on this tomorrow. ---------------------------------------------------------------------- PeterX · 2016-02-11 21:21 Quote from: wp on February 11, 2016, 04:46:13 pmIs your Lazarus the 32-bit or 64-bit version? I never use the 64 bit version. 32 bit must be enough ;-) ---------------------------------------------------------------------- wp · 2016-02-11 23:07 Quote from: PeterX on February 11, 2016, 10:19:43 pmI think about extracting the whole code of the Tree+ListView as a separate component - to make it usable in more than this current application. It may be less work to learn how to use VirtualTreeView, it combines a tree with a grid (which looks like a listview in report style), and it is an excellent component which is even used in commercial applications. See http://wiki.lazarus.freepascal.org/VirtualTreeview and http://wiki.freepascal.org/VirtualTreeview_Example_for_Lazarus (but there are also more detailed turorials for Delphi) ---------------------------------------------------------------------- PeterX · 2016-02-12 08:51 Hm, the VirtualTreeview looks very interesting. I am not in a hurry so I maybe go both ways. Keeping my program running is the main goal. But first I will try wp's  "Listview_Selected.zip" code to find out how it should work and why it doesn't in my app. ---------------------------------------------------------------------- PeterX · 2016-02-12 09:21 Okay, wp's  "Listview_Selected.zip" code works for me. Then I tried again my project ..  As this is an old Delphi project of mine, (written between 2000 and 2004 ..) I converted it with the Lazarus conversion tools. As there are several differences between LCL and VCL, lots of code didn't work as intended. The last days I edited a lot of code and .. however, this Code: [Select] ListView.ItemIndex:= i;works now !  I only wonder why MY selection is BLUE and the selection in wp's  "Listview_Selected.zip" code is light GRAY ? ---------------------------------------------------------------------- wp · 2016-02-12 09:46 Quote from: PeterX on February 12, 2016, 10:21:31 amI only wonder why MY selection is BLUE and the selection in wp's  "Listview_Selected.zip" code is light GRAY ?This is only a matter of focus. If the listview is focused then the selection is blue, otherwise it's gray. Just TAB through the controls on your form and see that the selection becomes blue only if the focus is at the listview (If the listview is the only control on the form then, of  course, the selection will always be blue). ---------------------------------------------------------------------- PeterX · 2016-02-12 10:23 Resolved now ! Thanks for all Your help and suggestions !