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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate controls and forms at run time

Status
Not open for further replies.

NeilFrank

Programmer
Mar 12, 2000
167
CA

Can someone point me to a good online or book-chapter discussion of how to create forms with controls (OptionButtons, Labels, TextBoxes etc) at runtime? I suspect Drag and Drop methods are used but it would be nice to have a look at the code behind some good examples.

Much appreciated.

 
Do you mean that you want to programatically generate a form? Or that you want a user to be able to build a form?
 
Good question.

I want the user to be able to build a form, using existing controls on other forms. It is sometimes referred to as building a template.

Thanks strongm.
 

If you explain what you over all GOAL is - maybe it will be easier to answer your question....

Unless you are building in VB the interface for the user to create forms with all controls for you (which is dangerous, your job may be obsolete ... :) )


Have fun.

---- Andy
 
Simple case:

Start: 1-form project
On the form there is a Command Button and an invisible Text Box

Open the project
Clicking the Command Button shows the Text Box
User can create a new form
And add to form a Command Button and a Text Box

Result: a 2-form project
Open the project
On each form there is a Command Button and an invisible Text Box
On each form clicking the Command Button shows the Text Box
 
>Result: a 2-form project
>Open the project

So you also want this to be persistent?
And do you want to be able to put usercode behind each control?
 

Is that what you are after?
(text box named Text1, Command1 and Command2 on the Form)
Code:
Option Explicit

Private Sub Form_Load()
    Text1.Visible = False
    Command1.Caption = "Show/Hide Text box"
    Command2.Caption = "Show Another Form"
End Sub

Private Sub Command1_Click()
    Text1.Visible =  Not Text1.Visible
End Sub

Private Sub Command2_Click()
    Dim frm As New Form1
    frm.Show
End Sub
Every time you click on the comand button "Show Another Form" - new form appears and you have a copy of your original Form.

Have fun.

---- Andy
 
Thanks, Andy

Clarification: I don't necessarily want the new form to be a carbon copy of the original. I'd like my user to be able to select elements from the original form and bring them - and their functionality - onto the new form. If this involves Drag & Drop routines, I know about these

BTW, I get a user-defined type not defined error on

Dim frm As New Form1.

Do I need to add a Component or Reference?

 
>I get a user-defined type not defined error on

If your Form is not called Form1 then you'll get this error. For example, if the form you want 'duplicated' is called frmTemplate then the code would be:

Dim frm as frmTemplate
 

But of course! Thanks.

I THINK I can take it from here, but just one more question: how do I save my new form in the project?

But perhaps I should restate my original question and relieve this Forum of a series of stupid questions:

Can someone point me to a complex example of what my little example tries to illustrate, ie where, at run time, having created a new form, multiple controls can be added via Drag & Drop and the resulting form saved as part of the project?
 
>the resulting form saved as part of the project?

Not really feasible once a project is compiled. Which isn't to say that you couldn't develop your own method for saving a form seperate from the project and being able to reload it at will with all its layout, methods, properties and events of itself and its (possibly nested) constituent controls - but it sure as heck won't be easy or straightforward.
 

I still can not see the reason of why you want to do it? Unless it is strictly an exercise in programming abilities. It looks to me you want to pass to your user the capabilities of creating the Form, all controls, plus some logic behind. But that’s supposed to be your (and our) job. That’s why what we do is called ‘programming’ and end user is called…. well, a user, not a programmer. :)

Have fun.

---- Andy
 
I am talking about a feature which has been referred to as 'Document Design' or creating 'customizable forms'.

An example can be seen at which illustrates new complex 'templates' being created and saved at runtime.

Yes, I suppose this is giving a user the ability to program, but in a limited way and only as a result of what strongm describes is a much higher level of programing on the programer's part.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top