Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Controls at Runtime

Status
Not open for further replies.

tsdragon

Programmer
Dec 18, 2000
5,133
US
Is there a way to create a copy of a control, or even a whole container full of controls at runtime?

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Cloning controls is actually quite a tricky concept. As a result, Microsoft has never builtiu it in by default into COM component5s (and hence VB controls). A keyword search in this forum should pick up some of my commentary about this.

Having said that, if a control supports persistence then it is pretty easy to turn that into a cloning function (and there are a few vVB controls and classes that have a built-in cloning function
 
I found an extremely easy answer to this question: use control arrays. If you create one of the control you want to clone on your form and give it an index of 0 (i.e. make it part of a control array) you can clone it all day long by simply using the Load command:
Code:
x = theControl.Count
Load theControl(x)
It creats ALMOST a clone of the original control. The Visible property is always false, and any actual data it contains (i.e. Caption, Text, Value, List, etc.) is not cloned, but almost every other attribute is.

Here's a very good tutorial on creating controls at runtime, both via this method and by the Controls.Add method:

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracy-

There is also faq222-1750 which details the same info as your link :)
 
... which isn't a copy or a clone, merely an instantiation of a class ...
 
(my point being that if you'd hadn't mentioned creating a copy in the original post we'd have probably gone straight to .Load and Control.add, but I thought you were looking for something else)
 
Sorry about the mixup in terminology. I started coding in COBOL on punched cards. I'm still not as comfortable with OOP terminology as some of you are (especially in VB). I was describing what I wanted to do from a physical point of view: I've got a set of controls in a frame, and I wanted to be able to "copy" that frame and it's set of controls repeatedly. Using Load did the job quite handily.

I guess if I was REALLY going object oriented I could have made the whole set of controls and their frame a class, but that's a little beyond my current level of VB expertise.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
No, no; no need to apologise. I was simply explaning why I went off in the direction I did
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top