Board index » delphi » Adding objects to a ComboBox list

Adding objects to a ComboBox list

I'm new to Delphi and having a problem adding objects to a ComboBox. The
problem is that my 'Object' is really a pointer to a record and it is this
pointer that I want to store in the list.

For example if I defined the record as:

type
        PRecord = ^TRecord;

        TRecord = record
                Field1, Field2...: Longint;
        end;

I don't understand after:

var
        ARecord: PRecord;
begin
        new(ARecord);

how I can add this pointer ARecord to the list:

        ComboBox.Items.AddObject(AString, ???);

I hope someone can help me (or even understand what I'm talking about!).

Thank you,

Jeremy.

 

Re:Adding objects to a ComboBox list


You could use Listview instead of Combobox. Listview allows
you to assign a pointer to the 'data' property in addition
to th rest..

Otherwise, just make sure your combobox indexitem maps to
your record index in some way, as in
'myrecord[combobox.indexitem+1]';

Beware,though, that you have to keep track of
insertions/deletions/sorting in BOTH your records and the
combobox...

Hope I've understood...

regards
 Kri Poulsen

Jeremy Hill <insi...@btinternet.com> wrote in article
<01bc078c$a1807ee0$3c2549c2@insight>...

Quote
> I'm new to Delphi and having a problem adding objects to
a ComboBox. The
> problem is that my 'Object' is really a pointer to a

record and it is
---
cut for space
----
Quote

> I hope someone can help me (or even understand what I'm
talking about!).

Re:Adding objects to a ComboBox list


Quote
"Jeremy Hill" <insi...@btinternet.com> wrote:
>I'm new to Delphi and having a problem adding objects to a ComboBox. The
>problem is that my 'Object' is really a pointer to a record and it is this
>pointer that I want to store in the list.
>For example if I defined the record as:
>type
>    PRecord = ^TRecord;
>    TRecord = record
>            Field1, Field2...: Longint;
>    end;
>I don't understand after:
>var
>    ARecord: PRecord;
>begin
>    new(ARecord);
>how I can add this pointer ARecord to the list:
>    ComboBox.Items.AddObject(AString, ???);
>I hope someone can help me (or even understand what I'm talking about!).

You need to typecast ARecord to a generic pointer:

ComboBox.Items.AddObject(AString, Pointer(ARecord));

Hope this helps.

Fredrik Nordbakke
FNProgramvare - Shareware to CATalog your collection
http://www.prodat.no/fnp/eng/

Re:Adding objects to a ComboBox list


Quote
I wrote:
> you can use the windows-api-msg: 'CB_SETITEMDATA' and 'GETITEMDATA' ...

that's a fault, of course, it must be 'CB_GETITEMDATA' instead to
retrieve the LongInt ;-)

regards

Peter

--
Peter Tuente     |   Email: Peter.Tue...@Materna.DE | The mission is:
Dr. Materna GmbH | Telefon: ++49 231 5599 308       | to boldly code
Vo?kuhle 37      |     Fax: ++49 231 5599 350       | where no one
D-44141 Dortmund |                                  | has man page 4

Re:Adding objects to a ComboBox list


Jeremy Hill <insi...@btinternet.com> wrote in article
<01bc078c$a1807ee0$3c2549c2@insight>...

Quote
> The problem is that my 'Object' is really a pointer to a
> record and it is ...

you can use the windows-api-msg: 'CB_SETITEMDATA' and 'GETITEMDATA'
to store and retrieve a 'LongInt'(32Bit) to an item of a combobox.
You can then cast the Longint to a pointer to access your Record ...

regards

Peter

--
Peter Tuente     |   Email: Peter.Tue...@Materna.DE | The mission is:
Dr. Materna GmbH | Telefon: ++49 231 5599 308       | to boldly code
Vo?kuhle 37      |     Fax: ++49 231 5599 350       | where no one
D-44141 Dortmund |                                  | has man page 4

Other Threads