Recent

Author Topic: Expanding a StdCtrl with a custom Property  (Read 2860 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Expanding a StdCtrl with a custom Property
« on: April 13, 2018, 01:06:14 pm »
Hi Folks,

I have a StdCtrl.TComboBox, which i have expanded with a custom Property using the Interposing-Class-Technique (at least that's what they called it on Stackoverflow).
It Works!

Now i want to add a StdCtrl.TListBox to my Project, which should also have that custom Property (exactly the same functionality).

I know that i could just start a second unit, copy/paste everything there, and be done with it, but somehow that just smells wrong to me.

So i was thinking: Both Controls have a common ancestor (TWinControl).
Ist it a good idea/possible to expand the ancestor incl. code for functionality?
I'm not really sure how to implement it, since with the Interposing-Technique i need to use the exactly same Type-Name.
Would the compiler recognize the Interposer?
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

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Expanding a StdCtrl with a custom Property
« Reply #1 on: April 13, 2018, 01:28:05 pm »
An interposer class is a runtime mechanism only. What that does, is to trick the streaming mechanism to create SecondaryUnit.class instead of an OriginalUnit.Class. What you ask with the TWinControls can not be done with out recompiling all its children.
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

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Expanding a StdCtrl with a custom Property
« Reply #2 on: April 13, 2018, 01:40:07 pm »
taazz,

thx for the reply.
I was kind of expecting it :/
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

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Expanding a StdCtrl with a custom Property
« Reply #3 on: April 13, 2018, 02:58:25 pm »
Ha! Solved it!
Switched from Interposer to Class Helper
Now it works.

What i don't understand:
I want to expand TListBox and TComboBox with an indexed Property (ItemData --> Yes, i admit it: I come from Visual Basic, and i love that Property).
Since i need an internal array to hold my values, i can only declare that Array after "implementation".
Is every instance of a ComboBox/ListBox going to "inherit" its own internal array?
I was kind of expecting the values being overwritten with the values of the second ComboBox after filling up the array with the values for my first combobox
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

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Expanding a StdCtrl with a custom Property
« Reply #4 on: April 13, 2018, 07:03:17 pm »
I'm not familiar with Visual Basic's ItemData. What type is the property, is it a pointer or a variant?

TStrings descendants (such as TListBox.Items or TComboBox.Items) have an Objects[] property you can use. It is designed for data classes (TObject-derived), but it can be adapted for any data container with a typecast.

TListBox does have a built-in Records[] indexed array, but it involves casting Items as a TExtendedStringList, and using the methods and properties of TExtendedStringList. I find it simpler just to use the Objects[] array in the parent TStrings class directly.

See the attached project for a simple example of adding and retrieving custom records for each listbox item. A similar approach works for TComboBox.Items as well.

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Expanding a StdCtrl with a custom Property
« Reply #5 on: April 13, 2018, 07:47:07 pm »
Howard,

ItemData (from Visual Basic resp. Visual Studio) is just an indexed Property consisting of Integers.

Yes, i know of the Object-Thingy, but for my purposes it just looks like overkill.

It's the classic usage of a combobox/listbox:
Get some Records from a Database (e.g. "SELECT ID, Description FROM MyTable ORDER BY Description")
Add the Description to be displayed in the ComboBox/ListBox, but add the ID (=Primary Key) into ItemData, which has the same sorting order.
Click on a Item in the Combo/List-Box, but retrieve the ID-Value from ItemData[ItemIndex].

That's all

EDIT: Right now i'm using it only on Combo/List-Boxes with "Sorted" set to False.
Anyone got an idea how the sorting is done?
« Last Edit: April 13, 2018, 08:04:03 pm by Zvoni »
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

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Expanding a StdCtrl with a custom Property
« Reply #6 on: April 13, 2018, 08:39:50 pm »
Object is just a typed pointer you add any kind of integer by casting it to a tobject eg TObject(Dataset.FieldByName('ID').asInteger); just make sure you do not forget your self and try to actually manage it as a TObject and you are set.

As for the sort its usually a quick sort routine that runs in a TStrings based internal variable that sorts based on the strings of the combo or the listbox for more info take a closer to the TStringlist class it has all the information you need.
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

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Expanding a StdCtrl with a custom Property
« Reply #7 on: April 13, 2018, 09:16:28 pm »
You can customise the TStrings sort using the CustomSort method.
See the attached example for one way to subclass a listbox to add an ID property which then becomes the basis for sorting.
It's an integer property, similar to the ItemData property you are used to, but named slightly more intelligently.

 

TinyPortal © 2005-2018