Expanding a StdCtrl with a custom Property lazarus / 31-LCL ---------------------------------------------------------------------- Zvoni · 2018-04-13 11:06 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? ---------------------------------------------------------------------- taazz · 2018-04-13 11:28 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. ---------------------------------------------------------------------- Zvoni · 2018-04-13 11:40 taazz, thx for the reply. I was kind of expecting it :/ ---------------------------------------------------------------------- Zvoni · 2018-04-13 12:58 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 ---------------------------------------------------------------------- howardpc · 2018-04-13 17:03 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 · 2018-04-13 17:47 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? ---------------------------------------------------------------------- taazz · 2018-04-13 18:39 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. ---------------------------------------------------------------------- howardpc · 2018-04-13 19:16 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.