| View previous topic :: View next topic |
| Author |
Message |
JVines Guest
|
Posted: Wed Sep 06, 2006 1:13 am Post subject: TCheckListBox Items tag |
|
|
What is the best way of attaching an int to each item in a TCheckListBox
or anything else that uses TStrings?
(I am populating a list box from a database table and would like to
associate an index with each item.) |
|
| Back to top |
|
 |
Clayton Arends Guest
|
Posted: Wed Sep 06, 2006 2:12 am Post subject: Re: TCheckListBox Items tag |
|
|
I use the "Object" member for that. Usually I reinterpret_cast a pointer to
my real struct associated with the string but it will work just fine for a
numerical value.
Strings->AddObject(str, index);
- Clayton |
|
| Back to top |
|
 |
JVines Guest
|
Posted: Wed Sep 06, 2006 8:11 am Post subject: Re: TCheckListBox Items tag |
|
|
This does not work. The item being associated with the string has to be
a TObject descendant. I need to be able to associate a simple int value. |
|
| Back to top |
|
 |
JD Guest
|
Posted: Wed Sep 06, 2006 1:40 pm Post subject: Re: TCheckListBox Items tag |
|
|
JVines <dev_support (AT) phoenixresearch (DOT) biz> wrote:
| Quote: |
This does not work.
|
The example that Clayton provided needed some casting but it
does work.
| Quote: | The item being associated with the string has to be a
TObject descendant.
|
To be more precise, is needs to be a TObject pointer.
| Quote: | I need to be able to associate a simple int value.
|
A pointer is 32 bits as is an int so just cast the int as a
TObject* when you set it and cast the TObject* as an int when
you get it. For example:
set
Strings->AddObject( "SomeString", reinterpret_cast<TObject*>(SomeIndex) );
or
Strings->Objects[x] = reinterpret_cast<TObject*>( SomeIndex );
get
int Index = reinterpret_cast<int>( Strings->Objects[x] );
~ JD |
|
| Back to top |
|
 |
JVines Guest
|
Posted: Fri Sep 08, 2006 2:58 am Post subject: Re: TCheckListBox Items tag |
|
|
[c++ Error] File.cpp(#): E2031 Cannot cast from 'Variant' to 'TObject *'
When the following line is used:
CheckListBox->Items->AddObject(TTable_Tbl->FieldValues["string"],
reinterpret_cast<TObject*>(TTable_Tbl->FieldValues["index"]);
Note: DataType for "index" is ftAutoInc |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Fri Sep 08, 2006 3:02 am Post subject: Re: TCheckListBox Items tag |
|
|
"JVines" <dev_support (AT) phoenixresearch (DOT) biz> wrote in message
news:4500961d$3 (AT) newsgroups (DOT) borland.com...
| Quote: | [c++ Error] File.cpp(#): E2031 Cannot cast from 'Variant' to 'TObject *'
When the following line is used:
CheckListBox->Items->AddObject(TTable_Tbl->FieldValues["string"],
reinterpret_cast<TObject*>(TTable_Tbl->FieldValues["index"]);
|
The FieldValues[] property uses a Variant. You cannot store that into the
TStrings::Objects[] property directly.
| Quote: | Note: DataType for "index" is ftAutoInc
|
ftAutoInc is a 32-bit integer value, so simply store the value of the
FieldValues[] property into an 'int' variable, and then store that into the
Objects[] property, ie:
int index = TTable_Tbl->FieldValues["index"];
CheckListBox->Items->AddObject(TTable_Tbl->FieldValues["string"],
reinterpret_cast<TObject*>(index));
Gambit |
|
| Back to top |
|
 |
DreamChaser Guest
|
Posted: Wed Feb 28, 2007 8:28 am Post subject: Re: TCheckListBox Items tag |
|
|
| Quote: |
When the following line is used:
CheckListBox->Items->AddObject(TTable_Tbl->FieldValues["string"],
You could change this part
reinterpret_cast<TObject*>(TTable_Tbl->FieldValues["index"]);
to: |
reinterpret_cast<TObject*>(TTable_Tbl->FieldByName("index")->AsInteger);
I believe :)
DC |
|
| Back to top |
|
 |
|