How to add new Property to ComboBox?

Hello,

Scripter and the controls provided in Scripter are dependent on built around the VCL counterparts.

Because of this some of the users of my product who have VB6 background are facing one problem and have been requesting me to add a new runtime property to ComboBox/ListBox control(s).

And this property is ItemData which will only store numeric values. As they are coming from VB6 background they are finding it hard and unintuitive to use ComboBox1.Items.AddObject syntax from Basic script.

I am fumbling with the code file ap_ComCtrls.pas to add this property but I am not able to make any headway.

Please help me in adding this runtime only property with read/write access.

Finally now can I compile the TMS Scripter Pro VCL after making changes to the base code?

Regards,

Yogesh

You can simply define that property as you would do with any new property. You don't need to change ap_ComCtrls for that. Just use DefineClass(TListBox).DefineProperty() and define your ItemData property the way you want: http://www.tmssoftware.biz/business/scripter/doc/web/acessingnon-publishedpropert.html.


To rebuild the packages you can use Package Rebuild Tool utility. It's installed when you install TMS Scripter and you can find an icon to launch it in the Windows start menu, All Programs, under "TMS Scripter" group.

Thanks for the code sample.

But I am not able to compile this code:

In Create event I have placed this code:
  //Code to add ItemData property to ComboBox control
 IDEScripter1.DefineClass(TListBox).DefineProp('ItemData',tkInteger,GetItemDataProc,SetItemDataProc);

I have created these two procedures:
procedure TForm1.GetItemDataProc(AMachine: TatVirtualMachine);
begin
   With AMachine do
      ReturnOutputArg(TListBox(CurrentObject).Items);   //<== Error occurs here
end;

procedure TForm1.SetItemDataProc(AMachine: TatVirtualMachine);
begin
   With AMachine do
      TListBox(CurrentObject):=GetInputArgAsInteger(0);   //<== Error occurs here
end;

I need to clarify that the property ItemData that I want to add will be for each Item in the Items collection of ComboBox control not just one single property for the control.

Regards,

Yogesh

Hello,

Would it be possible to add a method AddItem(S: string, ItemData: Integer) ?

By doing this now the ComboBox will have two AddItem methods one the default and another to add an Integer value

I hope I am making some sense?

Regards,

Yogesh

In this case I suggest you create an indexed property, not a regular property. This way your user can access the property using an index (ItemData[Index] for example).

Here is a sample: http://www.tmssoftware.biz/business/scripter/doc/web/registeringindexedproperties.html.

About the AddItem method, unfortunately Scripter doesn't support overloaded methods. You'd need to register it under a different name, like AddInteger or AddIntegerItem.