Recent

Author Topic: Duplicate listbox content: is this good?  (Read 1119 times)

dietmar

  • Full Member
  • ***
  • Posts: 170
Duplicate listbox content: is this good?
« on: October 02, 2021, 01:25:36 pm »
Hi,

I have a listbox which gets filled by the user with filenames. Since they can be quite long, I just write MinimizeName(filename) into the listbox. On the other hand, when working with the values, I need access to the whole long, complete filenames...

So my idea was to place a hidden listbox onto the form and to fill that parallel to the visible listbox, but with the real filenames, not MinimizeName.

Is that good style? Or has anyone a better idea for this problem?

Thx,
--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

y.ivanov

  • Sr. Member
  • ****
  • Posts: 306
Re: Duplicate listbox content: is this good?
« Reply #1 on: October 02, 2021, 01:39:30 pm »
Hi,

I have a listbox which gets filled by the user with filenames. Since they can be quite long, I just write MinimizeName(filename) into the listbox. On the other hand, when working with the values, I need access to the whole long, complete filenames...

So my idea was to place a hidden listbox onto the form and to fill that parallel to the visible listbox, but with the real filenames, not MinimizeName.

Is that good style? Or has anyone a better idea for this problem?

Thx,
--Dietmar

You can use an additional TStringList, not a whole list box in order to use it Items property.

wp

  • Hero Member
  • *****
  • Posts: 9051
Re: Duplicate listbox content: is this good?
« Reply #2 on: October 02, 2021, 02:24:24 pm »
The TListbox of Delphi has a virtual mode in which the listbox does not store the strings by itself but gets them from outside. This would be what you need, but in Lazarus this is not yet implemented.

If you do not insist on a TListbox you could switch to a TListView which can be virtualized. It means
- Store the strings in a StringList (or an array, or whatever suites you)
- Tell the ListView the count of the strings (ListView.Items.Count := <number of strings>)
- Set TListView.Ownerdata to true to switch to virtual mode
- Write an event handler for the ListView's OnData which passes a string from the StringList/Array/... to the specified Listview's item. Here you can call MinimizeName to shorten the string - the original string in the StringList/Array is unaffected.

Please study the attached demo, it contains lots of comments. Change the width of the form at runtime to see MinimizeName in action. And click on a list item to verify that the original full filename is returned although it is shortened in the ListView.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

dietmar

  • Full Member
  • ***
  • Posts: 170
Re: Duplicate listbox content: is this good?
« Reply #3 on: October 02, 2021, 02:39:01 pm »
Cool... thanks a lot! ;)

--Dietmar
Lazarus 2.2.0RC1 with FPC 3.2.2 (32 Bit) on Windows10 (64Bit)

Bart

  • Hero Member
  • *****
  • Posts: 4516
    • Bart en Mariska's Webstek
Re: Duplicate listbox content: is this good?
« Reply #4 on: October 02, 2021, 03:07:46 pm »
Make the listbox ownerdrawn, then draw the items with MiniMizeName.
Notice that what you see in the list is only the visual representation, Items
  • will still conatin the full name!


Bart

 

TinyPortal © 2005-2018