Board index » cppbuilder » Adding objects to ComboBox

Adding objects to ComboBox

I'd like to know if it's possible to use AddObject to efectively
add two strings to the same Item - one that appears in the
dropdown list and the other that exists as a string (not a
pointer to the string) as the object value.

If it's possible, how do I do it and given an index into the
objects, how do I fetch the object as a string and later delete
it?

TIA

~ JD

 

Re:Adding objects to ComboBox


Quote
"JD" <nos...@nospam.com> wrote in message

news:3e826ef1$1@newsgroups.borland.com...

Quote
> I'd like to know if it's possible to use AddObject
> to efectively add two strings to the same Item - one
> that appears in the dropdown list and the other that
> exists as a string (not a pointer to the string) as the
> object value.

You have to use a pointer for that.  Either dynamically allocate an instance
of AnsiString using the 'new' operator, ie:

    ComboBox1->Items->AddObject("Item 1", new AnsiString("Value 1"));
    ...
    delete (AnsiString*)ComboBox1->Items->Objects[0];

Or else store the string elsewhere, such as in an array, and then use the
ComboBox's index to look up the cooresponding value in the array.

    char* arr[3] = {"Value1", "Value 2", "Value 3"};

    ComboBox1->Items->Add("Item 1");
    ComboBox1->Items->Add("Item 2");
    ComboBox1->Items->Add("Item 3");
    ...
    AnsiString value = arr[ComboBox1->ItemIndex];

Gambit

Re:Adding objects to ComboBox


"Remy Lebeau \(TeamB\)" <gambi...@yahoo.com> wrote:

Quote
> You have to use a pointer for that.  Either dynamically
> allocate an instance of AnsiString using the 'new' operator, ie:

>    ComboBox1->Items->AddObject("Item 1", new AnsiString("Value 1"));
>    ...
>    delete (AnsiString*)ComboBox1->Items->Objects[0];

I like this solution - clean and neat. Assuming that I have the
actual string in another AnsiString, would the add look like this?

    ComboBox1->Items->AddObject("Item 1", new AnsiString(ExistingStringName));

And would using the string look like this?

         AnsiString *tmpString = (AnsiString*)ComboBox1->Items->Objects[0];
         if( tmpString == "SomeText" )
                   or
         ShowMessage( (AnsiString*)ComboBox1->Items->Objects[0] );

Thanks Gambit

~ JD

BTW - when do you sleep?

Re:Adding objects to ComboBox


Quote
"JD" <nos...@nospam.com> wrote in message

news:3e8282e1$1@newsgroups.borland.com...

Quote
> Assuming that I have the actual string in another
> AnsiString, would the add look like this?

Yes.  Though I left out a needed cast in my earlier sample:

    ComboBox1->Items->AddObject("Item 1", (TObject*)new
AnsiString(ExistingStringName));

Quote
>          AnsiString *tmpString =

(AnsiString*)ComboBox1->Items->Objects[0];

Quote
>          if( tmpString == "SomeText" )

Because it is a pointer, you need to dereference it before you can do
anything with it directly, ie:

    if( *tmpString == "SomeText" )

    ShowMessage( *(AnsiString*)ComboBox1->Items->Objects[0] );

Gambit

Other Threads