Board index » delphi » Override Combobox's Add Item Behaviour

Override Combobox's Add Item Behaviour

Hi,

    How do I go about overriding combobox's add item behaviour? I need to
process the item before it is added to the combobox but this process has to
be transparent to the user. This means that all the user has to do is to
call ComboBox1.Items.Add and the combobox will process the new item before
it is added to the list. Any help will be greatly appreciated. Thanks.

Regards,
Tan Tze Yong

 

Re:Override Combobox's Add Item Behaviour


On Mon, 9 Apr 2001 22:28:57 +0800, "Tan Tze  Yong"

Quote
<tytan1...@pacific.net.sg> wrote:
>Hi,

>    How do I go about overriding combobox's add item behaviour? I need to
>process the item before it is added to the combobox but this process has to
>be transparent to the user. This means that all the user has to do is to
>call ComboBox1.Items.Add and the combobox will process the new item before
>it is added to the list. Any help will be greatly appreciated. Thanks.

TComboBox doesn't have an add item method, it comes from the Items
property which is a descendant of TStrings.

What you'd need to do is create your own TStrings descendant; you can
model it on the TComboBoxStrings implementation in the StdCtrls unit
(unfortunately it's contained in the Implementation section so you
can't actually descend from it without changing VCL code) and put your
own code in an overridden .Add method.

Then you'd make your own TComboBox or TCustomComboBox descendant, and
instantiate your custom TStrings in an overridden contructor.  Since
the TCustomComboBox constructor already assigns a TComboBoxStrings
instance to Items, you'll want to free what it created and apply your
own, something like:

constructor TMyComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Items.Free;
  Items := TMyComboStrings.Create;
end;

Stephen Posey
slpo...@concentric.net

Re:Override Combobox's Add Item Behaviour


"Tan Tze Yong" <tytan1...@pacific.net.sg> wrote in message
news:9ash3d$mpi$1@newton.pacific.net.sg...

Quote
> Hi,

>     How do I go about overriding combobox's add item behaviour? I need to
> process the item before it is added to the combobox but this process has
to
> be transparent to the user. This means that all the user has to do is to
> call ComboBox1.Items.Add and the combobox will process the new item before
> it is added to the list. Any help will be greatly appreciated. Thanks.

> Regards,
> Tan Tze Yong

The combo bx data is probably set using a message. So you need to intercept
the message. Trace the .add code and see what message it sends. Then add a
message handler (look up the keyword "message") to your own ComboBox. The
message handler wil be called when the message comes trhough. I believe you
get called before the ancestor. Alternative override the wndproc and check
for the message in there.

Re:Override Combobox's Add Item Behaviour


On Mon, 9 Apr 2001 22:28:57 +0800, "Tan Tze  Yong"

Quote
<tytan1...@pacific.net.sg> wrote:
>Hi,

>    How do I go about overriding combobox's add item behaviour? I need to
>process the item before it is added to the combobox but this process has to
>be transparent to the user. This means that all the user has to do is to
>call ComboBox1.Items.Add and the combobox will process the new item before
>it is added to the list. Any help will be greatly appreciated. Thanks.

I know its not _real_ OO (<g>) but unless you want to totally hide the
TStrings.Add method, why not make a descendant of TComboBox which has an
"AddSpecial" method which does your pre-processing and then calls Items.Add.

Alan Lloyd
alangll...@aol.com

Other Threads