Recent

Author Topic: Filling a TCombobox(-like) GUI element from a list of objects?  (Read 4713 times)

feds

  • New Member
  • *
  • Posts: 33
Filling a TCombobox(-like) GUI element from a list of objects?
« on: December 17, 2017, 03:50:36 pm »
Hi all

Recently i have had to make a program at work using C#.

A very impressing feature was that i could use a list of objects to fill a combobox. The combobox has a property DataDource and a property DisplayMember.

I assigned the list of objects (all instances of the same class) to DataSource and set DisplayMember to the name (as string) of the property of the object that i want to see in the combobox. That's all.

Today i tried to find a way to do a similar thing in Lazarus but does not find any method to do it. I find some stuff about binding GUI elements to (database-) records but no way to bind to a list (or similar kind of container).

What's the recommendd way to do this in Lazarus?


Regards
Feds

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #1 on: December 17, 2017, 04:00:07 pm »
lazarus does not support this out of the box. You can try to find a object dataset which can be attached to an object list (or collection) and allow data aware controls to edit their properties or use some sort of ORM that has a automap between properties to editors and back.
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

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #2 on: December 17, 2017, 04:18:39 pm »
Just create a intermedia proxy class that allows you to add your objects as strings
and also have a property for the combobox you want it to be updated in.

 So each time you add an item to this this intermedia class it will automatically
update the Combobox strings. And the same holds true the other way round, when
the combobox makes changes it can trigger a method call within the Proxy class.
 
 The Proxy class can hold the items as references to the original objects in case you
want those to be updated if something in the ComboBox changes their values.

 I have done this many times with components and it woulds out very well. Its best
at times to write your own custom proxy class.

 the TDataSource in laz is for database operations and can be attached to a TDbComobobox, you can follow the examples there of that works and create your own DataSource for your needs to attached to a standard TComboBox.


The only true wisdom is knowing you know nothing

feds

  • New Member
  • *
  • Posts: 33
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #3 on: December 17, 2017, 04:30:14 pm »
the TDataSource in laz is for database operations and can be attached to a TDbComobobox, you can follow the examples there of that works and create your own DataSource for your needs to attached to a standard TComboBox.

Ups. Seems i was not clear about what i want. Using a TDbComboBox would be great. No need to stick with a TComboBox.

So all i need to do is to provide a proxy from the list to a TDataSource? Thats exactly what i'm looking for.
Are there any examples for anythink like this?

Regards
Feds


jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #5 on: December 17, 2017, 05:00:55 pm »
I don't think I miss understood you, you will find getting a TdbComboBox to do your
bidding is going to be a lot more work than simply writing a Proxy Tdatasource that
expects a standard Tcombobox.

  You will need to satisfy the duties of the Tdatasource for the TDBCombo box which
requires a TDataSet that is huge in its implementation.

 But if a challenge is what you are looking for, you have one  ;D
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #6 on: December 17, 2017, 05:26:08 pm »
If your objects, base class TMyObj, are stored in a TList (or so) then it's as simple as this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyObj = class (... TObject, or some descendant)
  3.     Name: String;  
  4.   end;
  5.  
  6. procedure ListToCombo(AList: TList; ACombo: TCombobox);
  7. begin
  8.   ACombo.Items.Clear;
  9.   for i := 0 to AList.Count-1 do
  10.     ACombo.Items.Add(TMyObj(AList[i]).Name);
  11. end;
To get more specific help with this approach you must specify how your "objects" are declared and in which kind of "list" they are stored.

Having the luxury of datasource etc will cost you many many more lines!

balazsszekely

  • Guest
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #7 on: December 17, 2017, 05:40:51 pm »
You can add any class or instance of a class to be more precise to a generic TList, or even better to TObjectList where you can specify FreeObjects, to automatically free the data. To assign the objects to a TComboBox just call the ComboBox1.Items.AddObject() function for each item.


feds

  • New Member
  • *
  • Posts: 33
Re: Filling a TCombobox(-like) GUI element from a list of objects?
« Reply #9 on: December 19, 2017, 07:17:00 pm »
But if a challenge is what you are looking for, you have one  ;D

Actually i'm more looking for solutions.
My DanceCard for callenges is still filled until December 2098  %)

Regards
Feds

 

TinyPortal © 2005-2018