BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Popupmenu creating control at runtime...How to check if inst

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Bcc32
Guest





PostPosted: Mon May 30, 2005 4:09 pm    Post subject: Popupmenu creating control at runtime...How to check if inst Reply with 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


Back to top
Antonio Felix
Guest





PostPosted: Mon May 30, 2005 5:11 pm    Post subject: Re: Popupmenu creating control at runtime...How to check if Reply with quote




"Bcc32" <bcc32 (AT) exe (DOT) org> 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

Back to top
Antonio Felix
Guest





PostPosted: Mon May 30, 2005 5:16 pm    Post subject: Re: Popupmenu creating control at runtime...How to check if Reply with quote




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

Back to top
Bcc32
Guest





PostPosted: Mon May 30, 2005 6:01 pm    Post subject: Re: Popupmenu creating control at runtime...How to check if Reply with quote


"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote

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



Back to top
JD
Guest





PostPosted: Tue May 31, 2005 5:26 am    Post subject: Re: Popupmenu creating control at runtime...How to check if Reply with quote


"Bcc32" <bcc32 (AT) exe (DOT) org> wrote:
Quote:

"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote in message
news:429b4a88$1 (AT) newsgroups (DOT) borland.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


Back to top
JD
Guest





PostPosted: Tue May 31, 2005 5:55 am    Post subject: Re: Popupmenu creating control at runtime...How to check if Reply with quote


"Bcc32" <bcc32 (AT) exe (DOT) org> 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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.