Recent

Author Topic: TListView - please advise - general ideas of working  (Read 658 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1095
TListView - please advise - general ideas of working
« on: April 28, 2025, 12:10:52 pm »
Today I need a practical idea, how to combine visual and non visual items in the case of a search function.

We start with the visual one:
The user clicks a search button next to a TEdit. He wants to search - let us say - for "animal".
What happens behind the scene: The search goes to a specialize unit, which knows about databases. This unit makes a query, finds the solution and gives it as lines of strings.
E.g. the user searches for animal, then the list would be humming bird, elephant and gorilla.

These "hits" are listed in a TListView (which is nice, but if somebody knows it better, I change it).
Now the user wants to work on with gorilla. He clicks it and the List-View-Click event finds the line.

Now I as programmer - am done:
How on earth was the primary key of gorilla?!!

What does not work is a pointer. Pls do not discuss it, it is not possible.

What I WANT to do is this:
I want the database unit to send something back, which contains the primary keys next to the found strings.
This can be done in several ways:
The ugly one: I use a value of the string list for it and cut it for the view.
This is very ugly, because I have the string values, cuts, conversion to figures again.
A minor ugly way would be to use a HashedStringList. Yes, is a string as well, but not THAT ugly.

So my question to the community (as the topic must be quite common):
Is there a nice solution (remember: pointer will not work) ?

If I do it with HashedStringList: How to combine the type with TListView?
Is there a way just to display ONE values without loosing the hashed value or must I keep my HashedStringList and search on the clickEvent for the second value (which would be my primary key)?



cdbc

  • Hero Member
  • *****
  • Posts: 2133
    • http://www.cdbc.dk
Re: TListView - please advise - general ideas of working
« Reply #1 on: April 28, 2025, 01:59:24 pm »
Hi
Quote
This unit makes a query, finds the solution and gives it as lines of strings.
If thereby you mean a stringlist, then it's easy-peasy:
Code: Pascal  [Select][+][-]
  1. // in special search unit...
  2. var
  3.   searchID: ptrint;
  4.   searchres: string;
  5. begin
  6.   /// first you search and get a string and an ID back ///
  7.   stringlist.AddObject(searchres,TObject(searchID));
  8.  
In your gui-unit:
Code: Pascal  [Select][+][-]
  1. var searchID: ptrint; searchres: string; {li: TListItem}
  2. begin
  3.   searchID:= ptrint(stringlist.Objects[i]);
  4.   searchres:= stringlist[i];
  5. ...
  6.   /// or:
  7.   li:= ListView.Add;
  8.   li.Text:= stringlist[i];
  9.   li.Data:= stringlist.Objects[i];
  10.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Khrys

  • Full Member
  • ***
  • Posts: 227
Re: TListView - please advise - general ideas of working
« Reply #2 on: April 28, 2025, 02:30:33 pm »
What does not work is a pointer. Pls do not discuss it, it is not possible.

Why not? Are you using composite primary keys or UUIDs that don't fit in a  PtrInt?

Nicole

  • Hero Member
  • *****
  • Posts: 1095
Re: TListView - please advise - general ideas of working
« Reply #3 on: April 28, 2025, 03:06:27 pm »
please NO pointer.
It does not work in this case.

It would be great, if you can help me to find a solution instead of pointing at paths leading to nowhere in my case.



Khrys

  • Full Member
  • ***
  • Posts: 227
Re: TListView - please advise - general ideas of working
« Reply #4 on: April 28, 2025, 03:21:08 pm »
please NO pointer.
It does not work in this case.

It would be great, if you can help me to find a solution instead of pointing at paths leading to nowhere in my case.

What are you trying to achieve? Benny's solution is simple and clear, and if that doesn't work for you, then please tell us why.

OC DelGuy

  • Full Member
  • ***
  • Posts: 208
  • 123
Re: TListView - please advise - general ideas of working
« Reply #5 on: April 28, 2025, 08:53:42 pm »
please NO pointer.
It does not work in this case.

It would be great, if you can help me to find a solution instead of pointing at paths leading to nowhere in my case.

What are you trying to achieve? Benny's solution is simple and clear, and if that doesn't work for you, then please tell us why.

For God's sake Khrys!  Dont do it!  Don't mention the "P" word!
Heav’n has no Rage, like Love to Hatred turn’d, Nor Hell a Fury, like a Woman scorn’d.

You've been warned!

 :D ;) :D ;) :D ;)    <Where is the ROTFL Smiley?>
« Last Edit: April 28, 2025, 08:58:07 pm by OC DelGuy »
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 4363
Re: TListView - please advise - general ideas of working
« Reply #6 on: April 29, 2025, 12:01:23 am »
Also puzzled about the pointer restriction part. In which case use the listview in virtual mode and map the items to the returned result(s) might be an option.

btw you can separate items in a stringlist using other methods as well, for example using a specific separator and split the string just before displaying or use 2 lines for each resulting entry. But that was more or less ruled out already.

Though I do agree with Khrys.

Is there a way just to display ONE values without loosing the hashed value or must I keep my HashedStringList and search on the clickEvent for the second value (which would be my primary key)?
If you do not map the database results (directly) to the listview then yes.

BTW: I just noticed, hashedstringlist ? What problem does that solve ? You might be better of using a map.
« Last Edit: April 29, 2025, 01:05:58 am by TRon »
Today is tomorrow's yesterday.

Nicole

  • Hero Member
  • *****
  • Posts: 1095
Re: TListView - please advise - general ideas of working
« Reply #7 on: April 29, 2025, 09:01:09 pm »
Thank you!
I was not aware, that map exists. This seems exactly the thing I have searched for. Google will explain to me how it works.
This HashedStringList feels that old, so I was quite sure, there shall be something else.
So cross fingers, it is solved. I'll try tomorrow.

TRon

  • Hero Member
  • *****
  • Posts: 4363
Re: TListView - please advise - general ideas of working
« Reply #8 on: April 29, 2025, 09:49:00 pm »
You're more than welcome Nicole.

Just in case googly is acting up and in case not familiar with generics:

Code: Pascal  [Select][+][-]
  1. program testmap;
  2.  
  3. {$mode objfpc}{$h+}
  4. {$warn 6058 off}
  5.  
  6. {
  7.   references:
  8.   - rtl-doc: https://www.freepascal.org/docs-html/rtl/fgl/tfpgmap.html
  9.   - wiki: https://wiki.freepascal.org/TFPGMap
  10. }
  11.  
  12. uses
  13.   classes, sysutils, fgl;
  14.  
  15.  
  16. type
  17.   TMyMap = specialize TFPGMap<int64, string>;  // <key, data>
  18.  
  19. var
  20.   Map : TMyMap;
  21.   n   : integer;
  22. begin
  23.   Map := TMyMap.Create;
  24.   try
  25.     // add
  26.     for n := 1 to 10 do
  27.       Map.Add(n, (n*100).ToString);
  28.  
  29.     // display
  30.     for n := 0 to Map.Count-1 do
  31.       writeln('key: ', Map.Keys[n], ' ->  value: ', Map.Data[n]);
  32.   finally
  33.     Map.Free;
  34.   end;
  35. end.
  36.  

It is just an arbitrary and simple example, please modify to personal needs.

There are other options available such as for example a (generic) objectlist or even something more sophisticated. It is just that I prefer the simplicity of the generic map.
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018