Board index » cppbuilder » TComboBox->Items->Objects[] woes

TComboBox->Items->Objects[] woes

I think I'm going doo-lally.

I fill a TComboBox (Style = csDropDownList) with 5 items using AddObject.  I
then execute the following code:

        int nCount = cboContactTypes->Items->Count;
        int nIndex = cboContactTypes->ItemIndex;
        ShowMessage( cboContactTypes->Items->Strings[nIndex] );
        int nContactTypeReq = (int)cboContactTypes->Items->Objects[nIndex];

nCount=5, nIndex = 0, the ShowMessage call displays the first item OK and
then the last line gives an error "List Index out of bounds (0)".

Huh!

I've ran it through Codeguard and got the de{*word*81} to stop on Exceptions,
but nothing happens until the line in question.  Can anyone spot something
obvious I've missed?

TIA

David

 

Re:TComboBox->Items->Objects[] woes


Quote
"DP" <daveperkins> wrote in message

news:3e36b7fa$1@newsgroups.borland.com...
Quote
>         int nContactTypeReq =

(int)cboContactTypes->Items->Objects[nIndex];

Quote

> nCount=5, nIndex = 0, the ShowMessage call displays the first item
OK and
> then the last line gives an error "List Index out of bounds (0)".

Did you store an Object value of -1 at any time?  The OS (i.e.
Windows) cannot handle this, either in direct API calls, or by the VCL
TComboBox wrapper.

--
Timothy H. Buchman
========================================
City Center Theater, New York NY
mail address tbuchmanPLEASE(at sign)REMOVEcitycenter.org
Search .borland message archive on http://www.mers.com/searchsite.html

Re:TComboBox->Items->Objects[] woes


Quote
"DP" <daveperkins@_woo-hoo"-'#-bigfoot.com> wrote:
> I fill a TComboBox (Style = csDropDownList) with 5 items
> using AddObject.  I then execute the following code:

>     int nCount = cboContactTypes->Items->Count;
>     int nIndex = cboContactTypes->ItemIndex;
>     ShowMessage( cboContactTypes->Items->Strings[nIndex] );
>     int nContactTypeReq = (int)cboContactTypes->Items->Objects[nIndex];

> nCount=5, nIndex = 0, the ShowMessage call displays the first
> item OK and then the last line gives an error "List Index out
> of bounds (0)".

It appears that your AddObject didn't work as you expected. Try this:

int x;
try
  {
  for(x = 0; x < cboContactTypes->Items->Count; x++)
    {
    ShowMessage( cboContactTypes->Items->Strings[x] );
    }
  }
catch (Exception &E)
  {
  MessageDlg(E.Message + " : x = " + IntToStr(x), mtError, TMsgDlgButtons() << mbOK, 0);
  }

and then try this:

int x;
try
  {
  for(x = 0; x < cboContactTypes->Items->Count; x++)
    {
    ShowMessage( cboContactTypes->Items->Strings[x] + " : " +
                 IntToStr((int)cboContactTypes->Items->Objects[x]));
    }
  }
catch (Exception &E)
  {
  MessageDlg(E.Message + " : x = " + IntToStr(x), mtError, TMsgDlgButtons() << mbOK, 0);
  }

~ JD

Re:TComboBox->Items->Objects[] woes


Quote
> Did you store an Object value of -1 at any time?

Bingo!  Yes I did.   I'll store -2 instead ;-)

Thanks.

Re:TComboBox->Items->Objects[] woes


Quote
> It appears that your AddObject didn't work as you expected. Try this:

Thanks for your reply.  You learn something new everyday.

Other Threads