Recent

Author Topic: [SOLVED] Listbox with TFrame's as elements?  (Read 4401 times)

feds

  • New Member
  • *
  • Posts: 33
[SOLVED] Listbox with TFrame's as elements?
« on: February 07, 2018, 01:43:10 pm »
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
« Last Edit: February 07, 2018, 09:15:30 pm by feds »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Listbox with TFrame's as elements?
« Reply #1 on: February 07, 2018, 02:03:18 pm »
Why don't you use a TScrollbox instead? Here you can add any other controls out of the box.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Listbox with TFrame's as elements?
« Reply #2 on: February 07, 2018, 05:49:09 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

  • New Member
  • *
  • Posts: 33
Re: Listbox with TFrame's as elements?
« Reply #3 on: February 07, 2018, 07:51:01 pm »
Why 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

  • New Member
  • *
  • Posts: 33
Re: Listbox with TFrame's as elements?
« Reply #4 on: February 07, 2018, 07:57:56 pm »
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.

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

  • Hero Member
  • *****
  • Posts: 5368
Re: Listbox with TFrame's as elements?
« Reply #5 on: February 07, 2018, 07:59:34 pm »
Because 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Listbox with TFrame's as elements?
« Reply #6 on: February 07, 2018, 08:13:35 pm »
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.
« Last Edit: February 07, 2018, 08:17:03 pm by wp »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Listbox with TFrame's as elements?
« Reply #7 on: February 07, 2018, 08:17:47 pm »
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

  • New Member
  • *
  • Posts: 33
Re: Listbox with TFrame's as elements?
« Reply #8 on: February 07, 2018, 09:08:30 pm »
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.

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
« Last Edit: February 07, 2018, 09:15:51 pm by feds »

feds

  • New Member
  • *
  • Posts: 33
Re: Listbox with TFrame's as elements?
« Reply #9 on: February 07, 2018, 09:14:19 pm »
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.

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  8-)

Greets
feds

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Listbox with TFrame's as elements?
« Reply #10 on: February 07, 2018, 09:38:42 pm »
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.
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
« Last Edit: February 07, 2018, 10:16:52 pm by wp »

 

TinyPortal © 2005-2018