Board index » cppbuilder » TListView OnInsert, how to get caption of item added

TListView OnInsert, how to get caption of item added

I'm trying to get the caption of the Item just added to the Listview.
The list view is set to vsReport and only has one column.  Here is the
code to populate the listview.

TListItem* LI;
for(int jj = 0; jj < 3; jj++)
{
   LI = ListView1->Items->Add();
   LI->Caption = IntToStr(jj);

Quote
}

My problem is the OnInsert() always returns NULL.

void __fastcall TForm1::ListView1Insert(TObject *Sender, TListItem *Item)
{
   ShowMessage(Item->Caption);  //Message box is empty.

Quote
}

thanks,
kc
 

Re:TListView OnInsert, how to get caption of item added


This should fix your problem:

void __fastcall TForm1::ListView1Insert(TObject *Sender, TListItem *Item)
{
   if (LI)
       ShowMessage(LI->Caption);

Quote
}

Bear in mind that the last inserted item won't be shown.

Rodolfo

Quote
"Casey McGuire" <k...@ruff.com> wrote in message

news:3e9187dd@newsgroups.borland.com...
Quote
> I'm trying to get the caption of the Item just added to the Listview.
> The list view is set to vsReport and only has one column.  Here is the
> code to populate the listview.

> TListItem* LI;
> for(int jj = 0; jj < 3; jj++)
> {
>    LI = ListView1->Items->Add();
>    LI->Caption = IntToStr(jj);
> }

> My problem is the OnInsert() always returns NULL.

> void __fastcall TForm1::ListView1Insert(TObject *Sender, TListItem *Item)
> {
>    ShowMessage(Item->Caption);  //Message box is empty.
> }

> thanks,
> kc

Re:TListView OnInsert, how to get caption of item added


Quote
"Casey McGuire" <k...@ruff.com> wrote in message

news:3e9187dd@newsgroups.borland.com...

Quote
> void __fastcall TForm1::ListView1Insert(TObject *Sender, TListItem *Item)
> {
>    ShowMessage(Item->Caption);  //Message box is empty.
> }

OnInsert is called at the point when the new TListItem is first being
created, within the context of Add() itself, before you have a chance to
assign any values to it afterwards.  That is why the Caption is always
empty.

You'll just have to delay your processing until after you have assigned
values to the TListItem:

    TListItem* LI;
    for(int jj = 0; jj < 3; jj++)
    {
        LI = ListView1->Items->Add();
        LI->Caption = jj;
        // do your processing of LI here, not in OnInsert...
    }

Gambit

Re:TListView OnInsert, how to get caption of item added


 >Remy Lebeau (TeamB) wrote:
Quote
> OnInsert is called at the point when the new TListItem is first being
> created, within the context of Add() itself, before you have a chance to
> assign any values to it afterwards.  That is why the Caption is always
> empty.

> You'll just have to delay your processing until after you have assigned
> values to the TListItem:

--------------------------------------------

Thanks for the help Remy and Rodolfo.  Why is TListItem even passed
to OnInsert() then?

Re:TListView OnInsert, how to get caption of item added


Quote
"Casey McGuire" <k...@ruff.com> wrote in message

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

Quote
> Why is TListItem even passed to OnInsert() then?

To let you know that a new item has been created in general.  The item
itself is valid, it just doesn't have any values assigned to it yet.  But
you can still access the TListItem itself, and do whatever you want with it.
Just because you haven't assigned any of your own values to it yet doesn't
mean the item itself is not valid or can't still be useful as-is.

Gambit

Other Threads