Board index » cppbuilder » Virtual Listview problems

Virtual Listview problems


2003-08-05 09:50:47 AM
cppbuilder90
I am trying to figure out this virtual listview, i am having some
problems
updating the items in the listview. I tonly seems to add in the
captions
and nothing else, can someone look and tell me where the problem
may be occuring.
struct TAddrItem
{
String Caption;
String Name;
String Address;
String City;
String State;
String Zip;
};
//--------------------------------------------------------------------
-------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
aList = new TList;
}
//--------------------------------------------------------------------
-------
void __fastcall TForm1::btnPopulateClick(TObject *Sender)
{
ClearList();
for(int i = 0; i < 11; ++i)
{
TAddrItem* ai = new TAddrItem;
ai->Caption = i;
ai->Name = "Names" + IntToStr(i);
ai->Address = "Address" + IntToStr(i);
ai->City = "City" + IntToStr(i);
ai->State = "State" + IntToStr(i);
ai->Zip = "Zip" + IntToStr(i);
// Add the item to the list.
aList->Add(ai);
}
ListView1->Items->Count = aList->Count;
}
//--------------------------------------------------------------------
-------
void __fastcall TForm1::ListView1Data(TObject *Sender, TListItem
*Item)
{
TAddrItem* ai = (TAddrItem*)aList->Items[Item->Index];
// Set the Caption and ImageIndex properties.
Item->Caption = ai->Caption;
Item->SubItems->Add(ai->Name);
Item->SubItems->Add(ai->Address);
Item->SubItems->Add(ai->City);
Item->SubItems->Add(ai->State);
Item->SubItems->Add(ai->Zip);
}
//--------------------------------------------------------------------
-------
void TForm1::ClearList()
{
for(int i = 0; i < aList->Count; i++)
delete((TAddrItem*)aList->Items[i]);
aList->Clear();
}
//--------------------------------------------------------------------
-------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
ClearList();
delete aList;
}
//--------------------------------------------------------------------
-------
This is the entire code to my little sample project.
Thanks in advance.....
 
 

Re:Virtual Listview problems

"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
I am trying to figure out this virtual listview, i am having
some problems updating the items in the listview. I tonly
seems to add in the captions and nothing else, can someone
look and tell me where the problem may be occuring.
Just to make sure, you are using the ListView in vsReport mode, correct?
The SubItems property only has meaning in vsReport mode. If so, there is
nothing wrong with the code you have posted otherwise, the columns should be
filled in correctly as expected.
Quote
void __fastcall TForm1::FormDestroy(TObject *Sender)
Do not use the OnDestroy event. It is a Delphi remnant that is unstable in
C++ as it can be triggered after the destructor, which is illegal in C++.
Use the destructor instead.
Gambit
 

Re:Virtual Listview problems

Quote
Just to make sure, you are using the ListView in vsReport mode,
correct?
The SubItems property only has meaning in vsReport mode. If so,
there is
I just checked again and its definitely in report mode, hmmmmmm.
Quote
nothing wrong with the code you have posted otherwise, the columns
should be
filled in correctly as expected.
Wow, i am really lost on this now.
Quote
Use the destructor instead.
Thanks for pointing that out......
I think i will have to redo the sample i am working on, even when i
populate
just the caption of the listview. I can't even select the items,
unless i need some
event for that too, well i am just learning about this. I am sure i
will have more
questions tomorrow, right now thats its for me.
Appreciate the help again.
 

{smallsort}

Re:Virtual Listview problems

See if my post helps
Rodolfo
Quote
I think i will have to redo the sample i am working on, even when i
populate
just the caption of the listview. I can't even select the items,
unless i need some
event for that too, well i am just learning about this. I am sure i
will have more
questions tomorrow, right now thats its for me.


Appreciate the help again.


 

Re:Virtual Listview problems

"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
Wow, i am really lost on this now.
I tried your example as-is, and it worked fine for me, I saw all of the data
correctly.
You did add actual columns to the ListView beforehand, didn't you?
SubItems->Add() only fills in the column data, but the columns themselves
must already exist in the ListView's Columns property ahead of time.
Quote
I can't even select the items, unless i need some event
for that too
There is nothing extra needed for that.
Gambit
 

Re:Virtual Listview problems

I ended up recreating the little demo and it worked, i don't
have a clue what could have gone wrong with the one i
was working on. Maybe i did something in object inspector
without realizing it.
I do appreciate all the help from you guys with this.
Quote
4- FormClose replaces FormDestroy (FormDestroy is unstable)
Rodolfo, is it better to use this event instead?
__fastcall ~TForm1();
//--------------------------------------------------------------------
-------
__fastcall TForm1::~TForm1()
{
ClearList();
delete aList;
}
//--------------------------------------------------------------------
-------
Thanks.
 

Re:Virtual Listview problems

Quote
You did add actual columns to the ListView beforehand, didn't you?
yea, believe me i did it all correctly, i may have accidently messed
up
something in object inspector without realizing it. I had to recreate
the
program and it worked (go figure).
Thanks for helping me out, now i at least have a clue on how to use
this
virtual listview.
 

Re:Virtual Listview problems

Great, thanks for explaining that.
Quote
1- The OnClose event handler for Form1 (FormClose()) will execute
before
~TForm1()
2- By using FormClose() you save typing the destructor ~TForm1()
 

Re:Virtual Listview problems

"Rodolfo Frino" < XXXX@XXXXX.COM >ha scritto:
Quote
1- The OnClose event handler for Form1 (FormClose()) will execute before
~TForm1()
2- By using FormClose() you save typing the destructor ~TForm1()

You can use either.

Why not keeping the TAddrItem in a std::vector and forgetting about
deallocation?
Also the construction would be more exception safe.
Rgds, Martin
 

Re:Virtual Listview problems

"Greg Stantin" < XXXX@XXXXX.COM >wrote in message
Quote
is it better to use this event instead?

__fastcall ~TForm1();
Yes.
Gambit
 

Re:Virtual Listview problems

"Rodolfo Frino" < XXXX@XXXXX.COM >wrote in message
Quote
No, it will produce the same effect, with these two minor points
Excuse me???? Using ~Tform1() will most certainly not have the same effect
as using the OnDestroy event. ~TForm1() is safe and preferred to use.
OnDestroy is unstable and should never be used.
Quote
1- The OnClose event handler for Form1 (FormClose())
will execute before ~TForm1()
OnClose is simply an event of the form's closure but is not always triggered
under certain circumstances. ~TForm1() is the form's actual destructor and
is guaranteed to always execute regardless of how the form is
closed/destroyed.
Quote
2- By using FormClose() you save typing the destructor ~TForm1()
Only because the IDE can generate the code for you. That doesn't mean it is
any safer to use then the destructor itself. Besides, you can always
utilize copy/paste to copy the constructor declaration to create the
destructor declaration. In the time it takes you to focus the form, bring
up the Object Inspector, go to the Events tab, and double-click the OnClose
entry, you could just as easily have a working destructor set up, with maybe
a few seconds difference at most.
Gambit
 

Re:Virtual Listview problems

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Rodolfo Frino" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>No, it will produce the same effect, with these two minor points

Excuse me???? Using ~Tform1() will most certainly not have the same
effect
as using the OnDestroy event. ~TForm1() is safe and preferred to use.
OnDestroy is unstable and should never be used.
The question I replied to was not about OnDestroy but about OnClose.
If you read my post again you will find that in one of my points (point 4)
I mentioned the fact that the OnDestroyevent handler (FormDestroy) is
unstable
"4- FormClose replaces FormDestroy (FormDestroy is unstable)"
Quote
>1- The OnClose event handler for Form1 (FormClose())
>will execute before ~TForm1()

OnClose is simply an event of the form's closure but is not always
triggered
under certain circumstances.
Under which circumstances?
Rodolfo
 

Re:Virtual Listview problems

"Rodolfo Frino" < XXXX@XXXXX.COM >wrote in message
Quote
The question I replied to was not about OnDestroy
but about OnClose.
That is not the comment I was referring to. Greg asked you:
"Rodolfo, is it better to use this event instead?
__fastcall ~TForm1();"
You said:
"No, it will produce the same effect"
That is what my previous reply was commenting on.
Quote
Under which circumstances?
OnClose is only triggered when TCustomForm::Close() is called. Close() is
not the only way a form is closed. If a form is destroyed directly, via the
'delete' operator or its Free() or Release() methods, then the OnClose event
is not triggered, but the destructor still is. There are places inside the
VCL where forms are freed directly, they do not use Close(). Offhand, I do
not recall where they are, but I know that I have seen them before.
Gambit
 

Re:Virtual Listview problems

Quote
That is not the comment I was referring to. Greg asked you:

"Rodolfo, is it better to use this event instead?
__fastcall ~TForm1();"

You said:

"No, it will produce the same effect"
If you read this again
">4- FormClose replaces FormDestroy (FormDestroy is unstable)
Rodolfo, is it better to use this event instead?"
What Greg meant was
"is it better to use this event instead [ ~TForm1()] [rather than OnClose]?"
He was asking about the difference of using either the Form destructor or
FormClose
to do the cleanup. The issue about FormDestroy was already addressed in the
same point (4)
Quote
>Under which circumstances?

OnClose is only triggered when TCustomForm::Close() is called. Close() is
not the only way a form is closed. If a form is destroyed directly, via
the
'delete' operator or its Free() or Release() methods, then the OnClose
event
is not triggered, but the destructor still is. There are places inside
the
VCL where forms are freed directly, they do not use Close(). Offhand, I
do
not recall where they are, but I know that I have seen them before.
OK, I believe you. But you could always test the OnClose event handler to
see
if it is executed or not by placing a MessageBox/ShowMessage call in it.
You can also prevent the OnClose event from executing by simply
assigning NULL:
Form1->OnClose = NULL;
Of course, this has to be done intentionally.
Rodolfo
 

Re:Virtual Listview problems

Also, if you call Application->Terminate() the OnClose event handler
fot the main form will not be called, however the destructor ~TForm1() will
still be called
to do any necessary cleanup:
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Application->Terminate();
}
Perhaps the VCL code uses Free() somewhere here?
Rodolfo