Discussion:
problems in the AddObject() ?
(too old to reply)
chenzero
2008-05-24 21:09:07 UTC
Permalink
Hi,

I have a application that, a combobox on it, I want to associate
each items in the combobox with an index in other data.
however, it seemed that it can not associate -1 with the item.

the code is below,

// if -1 in the associated object, if will error in ComboBox1Change() !!!
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i;
for (i=-1;i<5;i++) {
ComboBox1->Items->AddObject(IntToStr(i), (TObject*)i);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
int ii = ComboBox1->ItemIndex;
if (ii<0) return;
TObject* p = (ComboBox1->Items->Objects[ii]);
int c = (int)p;
ShowMessage(IntToStr(c));
}
//---------------------------------------------------------------------------

Any idea? Thanks!
chenzero
chenzero
2008-05-24 21:20:16 UTC
Permalink
// more specific, the following function is the Change event handler of
// ComboBox1
Post by chenzero
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
int ii = ComboBox1->ItemIndex;
if (ii<0) return;
TObject* p = (ComboBox1->Items->Objects[ii]);
int c = (int)p;
ShowMessage(IntToStr(c));
}
//---------------------------------------------------------------------------
Thanks!
Remy Lebeau (TeamB)
2008-05-27 20:47:09 UTC
Permalink
Post by chenzero
I have a application that, a combobox on it, I want to
associate each items in the combobox with an index in
other data. however, it seemed that it can not associate
-1 with the item.
Actually, you can store -1 fine. You just cannot retreive it back later,
as -1 is the same value that the CB_GETITEMDATA message returns when an
error occurs. What you could do, however, is store 1-based values instead
of 0-based values, ie:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i = -1; i < 5; ++i)
ComboBox1->Items->AddObject(i, reinterpret_cast<TObject*>(i+1));
}

void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
int ii = ComboBox1->ItemIndex;
if( ii >= 0 )
{
int i = reinterpret_cast<int>(ComboBox1->Items->Objects[ii])-1;
ShowMessage(i);
}
}


Gambit
chenzero
2008-05-28 15:04:49 UTC
Permalink
Hi Remy,
Thanks, that's good idea. myself workaround is to store a relative
impossible big value for that. obviously, your solution is more better.
Thanks!
chenzero
Post by Remy Lebeau (TeamB)
Post by chenzero
I have a application that, a combobox on it, I want to
associate each items in the combobox with an index in
other data. however, it seemed that it can not associate
-1 with the item.
Actually, you can store -1 fine. You just cannot retreive it back later,
as -1 is the same value that the CB_GETITEMDATA message returns when an
error occurs. What you could do, however, is store 1-based values instead
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i = -1; i < 5; ++i)
ComboBox1->Items->AddObject(i,
reinterpret_cast<TObject*>(i+1));
}
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
int ii = ComboBox1->ItemIndex;
if( ii >= 0 )
{
int i = reinterpret_cast<int>(ComboBox1->Items->Objects[ii])-1;
ShowMessage(i);
}
}
Gambit
Loading...