Recent

Author Topic: ListView - store id inside elements  (Read 1656 times)

noszone

  • New Member
  • *
  • Posts: 46
ListView - store id inside elements
« on: January 25, 2023, 01:05:26 pm »
I have some data set like:

id 12323, name - John,
id 12324, name - Bob,
...

I want to store these ids in each item of ListView elements.

So when I want to read id, something like:

Code: Pascal  [Select][+][-]
  1. id:=ListView1.Items[i].index?

But in this case it gives only incremental id like 0,1,2,3,4,5..

I've tried:

Code: Pascal  [Select][+][-]
  1. ListView1.AddItem('test',TObject(3));

But seems it's wrong.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2081
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ListView - store id inside elements
« Reply #1 on: January 25, 2023, 01:34:14 pm »
I do not know if I understand you correct ....
Create a Column for it, add to that your ID, either set Visible to True or False if you want to display or just store it.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: ListView - store id inside elements
« Reply #2 on: January 25, 2023, 01:39:18 pm »
One thing is for sure: you can not use the index to abuse for your own amusement. A listview is a collection and the index refers to it's index in the list..

It is usually ok to abuse attached objects (such as in a stringlist) to contain a number (be very careful with casting). But it is not considered neat programming. I have no idea if tlistview allows that.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2081
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ListView - store id inside elements
« Reply #3 on: January 25, 2023, 01:42:10 pm »
To store:
Code: Pascal  [Select][+][-]
  1.        with YourListView.Items.Add do
  2.         begin
  3.           Caption := 'your main caption';
  4.           SubItems.Add('your id as text');
  5.         end;
To read:
Code: Pascal  [Select][+][-]
  1. s := YourListView.Items.Item[TheIndexPosition].SubItems[0];
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

noszone

  • New Member
  • *
  • Posts: 46
Re: ListView - store id inside elements
« Reply #4 on: January 25, 2023, 01:43:46 pm »
Thank you, I missed, don't know the columns are present. I will try also idea with subitems.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2081
  • Fifty shades of code.
    • Delphi & FreePascal
Re: ListView - store id inside elements
« Reply #5 on: January 25, 2023, 01:50:55 pm »
Thank you, I missed, don't know the columns are present. I will try also idea with subitems.
In my above snippet, there you should have already a tlistview with at least 2 columns.
The first column you can access direct, the 2nd is than "SubItem[0]" and so on...

You are welcome!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: ListView - store id inside elements
« Reply #6 on: January 25, 2023, 01:52:57 pm »
Nevermind that using a ListView for something like this is actually a misuse.
Use TStringGrid instead
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

noszone

  • New Member
  • *
  • Posts: 46
Re: ListView - store id inside elements
« Reply #7 on: January 25, 2023, 02:23:00 pm »
What if I want to use CheckListBox? Is something similar is possible? I guess the CheckListBox is more user-friendly when selecting multiple items, while in ListView Ctrl key on keyboard must be hold always.

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: ListView - store id inside elements
« Reply #8 on: January 25, 2023, 02:36:50 pm »
What if I want to use CheckListBox? Is something similar is possible? I guess the CheckListBox is more user-friendly when selecting multiple items, while in ListView Ctrl key on keyboard must be hold always.
A TCheckListBox has "Items" which are TStrings, which in turn have AddObject.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: ListView - store id inside elements
« Reply #9 on: January 25, 2023, 02:38:52 pm »
Storing extra data in a visual control is bad design anyway. Better to do that in the TStrings, so you can assign that to any control with a Tstrings property (strings, items).
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: ListView - store id inside elements
« Reply #10 on: January 25, 2023, 02:44:51 pm »
Storing extra data in a visual control is bad design anyway. Better to do that in the TStrings, so you can assign that to any control with a Tstrings property (strings, items).
Thaddy, at a guess: OP's coming from Visual Basic (or similiar), and there List-Controls (ComboBox, ListBox) do in fact have a separate property for stuff like that (if memory serves called "ItemData" -DataType Integer) which is even "sorting-aware".

That said: Agree with you
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: ListView - store id inside elements
« Reply #11 on: January 25, 2023, 02:47:10 pm »

Thaddy, at a guess: OP's coming from Visual Basic (or similiar), and there List-Controls (ComboBox, ListBox) do in fact have a separate property for stuff like that (if memory serves called "ItemData" -DataType Integer) which is even "sorting-aware".
The objects property is already in TStrings and is equal to ItemData.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

balazsszekely

  • Guest
Re: ListView - store id inside elements
« Reply #12 on: January 25, 2023, 03:32:17 pm »
What if I want to use CheckListBox? Is something similar is possible? I guess the CheckListBox is more user-friendly when selecting multiple items, while in ListView Ctrl key on keyboard must be hold always.
Yes it's possible. See attached project.

cdbc

  • Hero Member
  • *****
  • Posts: 1090
    • http://www.cdbc.dk
Re: ListView - store id inside elements
« Reply #13 on: January 25, 2023, 05:22:53 pm »
Hi
To store an integer value, quick'n'dirty:
Code: Pascal  [Select][+][-]
  1. ListItem.Data:= pointer(111); // or pointer(Int1);
  2.  
and to get it back again:
Code: Pascal  [Select][+][-]
  1. var Int1: ptrint;
  2. ...
  3. Int1:= ptrint(ListItem.Data);
  4.  
Hth - Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

noszone

  • New Member
  • *
  • Posts: 46
Re: ListView - store id inside elements
« Reply #14 on: January 25, 2023, 08:33:54 pm »
Thanks for all replies, I got new stuff to test and new ideas.

I tested some of them, and end up with something like this for now:

Code: Pascal  [Select][+][-]
  1. CheckListBox1.Items.AddObject('test',Tobject(232323)); //id from database to keep inside element
  2. showmessage(inttostr(integer(CheckListBox1.Items.Objects[0]))) //reading the value 232323 from CheckListBox

Hope it will work in loop, I didn't end the whole code yet. I'm storing these ID's in visual element, cause I will need to know which elements from CheckListBox a user has been selected.
« Last Edit: January 25, 2023, 08:36:58 pm by noszone »

 

TinyPortal © 2005-2018