Board index » cppbuilder » Popupmenu creating control at runtime...How to check if instance exists?

Popupmenu creating control at runtime...How to check if instance exists?


2005-05-31 12:09:10 AM
cppbuilder18
Hello All,
I am trying to create a second treeview at runtime from a popup menu.
If the runtime-user clicks the menuitem twice, how do I check if the second
treeview has
already been created without getting Access Violation at runtime?
This is what I tried...
bool isinstance;
if(Form1->Components[c]->Name.AnsiCompare("TreeView2")==0)
{
isinstance=true;
}
else
{
isinstance=false;
}
}
if(isinstance==true)
{
TreeView2->Items->Clear();
//does not compile...If i add TTreeView *Treeview2 to header or local
function i get AV
}
else
{
TTreeView *TreeView2=new TTreeView(Form1);
}
TreeView2->Parent=Panel1;
TreeView2->Left=TreeView1->Width;
TreeView2->Top=TreeView1->Top;
TreeView2->Width=TreeView1->Width;
TreeView2->Height=TreeView1->Height;
What is the best way to check for runtime existance of runtime created
control?
Thanks
Bcc32
 
 

Re:Popupmenu creating control at runtime...How to check if instance exists?

"Bcc32" < XXXX@XXXXX.COM >wrote:
Quote
Hello All,
I am trying to create a second treeview at runtime from a popup menu.
If the runtime-user clicks the menuitem twice, how do I check if the second
treeview has
already been created without getting Access Violation at runtime?
This is what I tried...

bool isinstance;
if(Form1->Components[c]->Name.AnsiCompare("TreeView2")==0)
{
isinstance=true;
}
else
{
isinstance=false;
}
}
if(isinstance==true)
{
TreeView2->Items->Clear();
//does not compile...If i add TTreeView *Treeview2 to header or local
function i get AV
}
else
{
TTreeView *TreeView2=new TTreeView(Form1);
}

TreeView2->Parent=Panel1;
TreeView2->Left=TreeView1->Width;
TreeView2->Top=TreeView1->Top;
TreeView2->Width=TreeView1->Width;
TreeView2->Height=TreeView1->Height;

What is the best way to check for runtime existance of runtime created
control?

Thanks

Bcc32


Hi,
Set a Global TTreeView *TreeView2 in the Forms Header File
On the Form Constructor set it to NULL
TreeView2 = NULL;
Then check if it is NULL when needed
if (!TreeView2) {
// Create
TreeView2 = new TTreeView( this ); // if this is Form1
TreeView2->Parent=Panel1;
TreeView2->Left=TreeView1->Width;
TreeView2->Top=TreeView1->Top;
TreeView2->Width=TreeView1->Width;
TreeView2->Height=TreeView1->Height;
} else {
// Already created
TreeView2->Items->Clear();
...
}
HTH
Antonio
 

Re:Popupmenu creating control at runtime...How to check if instance exists?

Quote
Hi,

Set a Global TTreeView *TreeView2 in the Forms Header File

Sorry, it should be a Forms Private TTreeview not a Global one
Br
Antonio
 

{smallsort}

Re:Popupmenu creating control at runtime...How to check if instance exists?

"Antonio Felix" < XXXX@XXXXX.COM >wrote in message
Quote

>Hi,
>
>Set a Global TTreeView *TreeView2 in the Forms Header File
>

Sorry, it should be a Forms Private TTreeview not a Global one

Br
Antonio
Thanks Antonio
You steered me in the right direction...
I forgot to change one thing that caused AV...
I was overlooking the double declaration of TTreeView2...
//in header
TTreeView *TreeView2;
//in constructor
TTreeView2=NULL;
//in cpp
TTreeView *TreeView2=new TTreeView(Form1);
//which should be
TreeView2 = new TTreeView(Form1);
hmm... I wonder why the compiler didnt complain???
Thanks for helping
Bcc32
 

Re:Popupmenu creating control at runtime...How to check if instance exists?

"Bcc32" < XXXX@XXXXX.COM >wrote:
Quote

"Antonio Felix" < XXXX@XXXXX.COM >wrote in message
news:429b4a88$ XXXX@XXXXX.COM ...
>
>Sorry, it should be a Forms Private TTreeview not a Global one
That is still global to the class (form).
Quote
TTreeView *TreeView2=new TTreeView(Form1);
//which should be
TreeView2 = new TTreeView(Form1);

hmm... I wonder why the compiler didnt complain???
Because that's a valid statement and a memory leak to boot.
The reason that it didn't work for you is because any reference
to the variable name 'TreeView' inside the constructor refers
to the pointer defined inside the constructor. Any other
reference to the variable name 'TreeView' *outside* of the
constructor refers to the other pointer that is defined in the
header.
The Access Violation was caused by your code external from the
constructor that derefrenced the global-to-the-form pointer
'TreeView'. That pointer is/was uninitialized and the compiler
knows that you have no business even looking at that address
(that's the Access Violation).
~ JD
 

Re:Popupmenu creating control at runtime...How to check if instance exists?

"Bcc32" < XXXX@XXXXX.COM >wrote:
Quote

I am trying to create a second treeview [...] how do I check
if the second treeview has already been created [...]
If you want the TTreeView to be allocated for the duration of
the form, then allocate it in the form's constructor but take
care to add code so that it doesn't gobble memory.
Your alternative is to allocate it when the user wants it and
to deallocate it when the user, or you, are finished using it
(you can add a little close button).
Presuming that you have initialized the pointer to NULL in the
ctor, the MenuClick would look like:
if( !TreeView2 ) // if TreeView == NULL
{
// allocate second instance
TreeView = new TTreeView( this );
// set properties as needed
}
PopulateTreeView2();
and the deletion would look like:
delete TreeView2;
TreeView2 = NULL;
Note that you don't need to worry about deleting the object to
prevent a memory leak because the object is assigned an Owner
when it's allocated. The Owner assumes responsibility for
destroying all of the objects that it ownes (and they in turn
are responsible for destroying all of the objects that they
own and so on).
IOW, if the user terminates the application, the TTreeView
will be destroyed by the VCL in an orderly fashion.
~ JD