[SOLVED] Listbox with TFrame's as elements? lazarus / 31-LCL ---------------------------------------------------------------------- feds · 2018-02-07 12:43 Hi all How can i use other items than strings in a TListbox? To be more precious i have a bunch of TFrame decendents that shoud be shown as a list on a TForm. While searching for this i got the impression that it would not be very complicated to make a similar setup with a TTreeview because this component provides an AddObject() method. Unfortunally TListbox seems not provide this :/ Any help is highly recommended Greets feds ---------------------------------------------------------------------- wp · 2018-02-07 13:03 Why don't you use a TScrollbox instead? Here you can add any other controls out of the box. ---------------------------------------------------------------------- howardpc · 2018-02-07 16:49 Quote from: feds on February 07, 2018, 01:43:10 pm i have a bunch of TFrame decendents that shoud be shown as a list on a TForm. If you want frames listed by name in a listbox, whose names point to their respective instances then you may be looking for something like the example attached. ---------------------------------------------------------------------- feds · 2018-02-07 18:51 Quote from: wp on February 07, 2018, 02:03:18 pmWhy don't you use a TScrollbox instead? Here you can add any other controls out of the box. Because i would like to avoid the positioning calculations needed here. Mostly because new frames are generated during runtime and may have different heights. I was hoping that there is a class (or kind of trick) that handles this stuff for all TComponents in the same manner as TListview does for strings. Maybe i should have a closer look to TCustomListview and try to implement that class? Greets feds ---------------------------------------------------------------------- feds · 2018-02-07 18:57 Quote from: howardpc on February 07, 2018, 05:49:09 pmIf you want frames listed by name in a listbox, whose names point to their respective instances then you may be looking for something like the example attached. Unfortunally not  A TListview views a list of strings. I'm looking for a way to view frames instead of the strings. Greets feds ---------------------------------------------------------------------- taazz · 2018-02-07 18:59 Quote from: feds on February 07, 2018, 07:51:01 pmBecause i would like to avoid the positioning calculations needed here. Mostly because new frames are generated during runtime and may have different heights. There is no way around the calculations,  set the frame.align to altop, you only need to set the top property of the frame low enough to make sure that it gets added last. ---------------------------------------------------------------------- wp · 2018-02-07 19:13 You can top-align them (Align := alTop), then you don't have to calculate the coordinates. But note that Lazarus does not add them in the correct order if they are added consecutively and their Top values are equal. Just increment Top to some value higher than the Top assigned to the previously added frame (need not be the "correct" top) - see attached demo which adds runtime-generated frames this way. If you add a frame later set its top to a very large value, e.g. 9999. ---------------------------------------------------------------------- howardpc · 2018-02-07 19:17 If alTop is not suitable (because it may mess with the width of the frames) you can use anchoring. As your code generates each new frame, simply anchor its top either to the form (for the first one) or to the bottom of the previous frame (for all successive frames). With anchoring, the Height of the anchored control is irrelevant. ---------------------------------------------------------------------- feds · 2018-02-07 20:08 Quote from: wp on February 07, 2018, 08:13:35 pmYou can top-align them (Align := alTop), then you don't have to calculate the coordinates. But note that Lazarus does not add them in the correct order if they are added consecutively and their Top values are equal. Just increment Top to some value higher than the Top assigned to the previously added frame (need not be the "correct" top) - see attached demo which adds runtime-generated frames this way. If you add a frame later set its top to a very large value, e.g. 9999. Wow. Works like a charm  I didn't know that top-align works when the top is incremented by only one. That makes it very handy because i can use the componeent count of the scrollbox to get the next top coordinate. Hence no need to maintain this by hand. Thanks a lot for the help  Greets feds ---------------------------------------------------------------------- feds · 2018-02-07 20:14 Quote from: howardpc on February 07, 2018, 08:17:47 pmIf alTop is not suitable (because it may mess with the width of the frames) you can use anchoring. As your code generates each new frame, simply anchor its top either to the form (for the first one) or to the bottom of the previous frame (for all successive frames). With anchoring, the Height of the anchored control is irrelevant. Very good hint. Thanks a lot  Actually all frames have the same width but i'm not brave enough to guarantee this in the future  Greets feds ---------------------------------------------------------------------- wp · 2018-02-07 20:38 Quote from: feds on February 07, 2018, 09:08:30 pmI didn't know that top-align works when the top is incremented by only one. That makes it very handy because i can use the componeent count of the scrollbox to get the next top coordinate. Hence no need to maintain this by hand.In principle, it should work also if Top were set to the same large value every time a frame is added. At least this works in Delphi. But Lazarus seems to have a bug here and adds them in reverse order. See also https://stackoverflow.com/questions/47429176/different-results- of-tscrollbox-population-in-delphi-and-lazarus