• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Creating new forms from code

Hi!

How can I create new forms or any other objects only from code?

I have seen somewhere, that this can be done by using New keyword, but I don't know how exactly it works.

This is what I have so far:

    Dim form2 As New Form1
    Load form2
    form2.Show

This creates new form which looks like form1, but I want to create completely new and empty form.
Can I create an array of forms?      form(0), form(1), form(2) . . .
How do I change properties of new forms?    My example gives 'Object Required' error when I try:     form2.caption = "abcdefg"

If possible, can you give me code example that would do the following:

- create 2 new empty forms, and display them
- change caption for first form
- create button on second form

Thanx!
0
dbrckovi
Asked:
dbrckovi
1 Solution
 
Crazy_king_inCommented:
Create a new form named Form1

Have a command button, paste the following code in the click event of the command button, u can see what u want


Dim J As New Form1
Dim K As New Form1
J.Show
K.Show
J.Caption = "First Form - Empty"

K.Caption = "Second Form"
K.Move 0, 0
K.Controls.Add "VB.TextBox", "cmdObj1", K
With K!cmdObj1
    .Visible = True
    .Width = 2000
    .Text = "Dynamic TextBox"
End With
0
 
MaxPolCommented:
You can have index for new forms.

Create a form called Form1.

Place whatever you nee inside

Then declare the following

Dim MyForm(x) as Form1 ' where x is a value, but you could also define it like Dim MyForm() as Form1 and then redim it


' index is a value from 0 to x-1
Set MyForm(index) = New Form1

Then you can access all objects as follow:


MyForm(0).label1.caption="Text Here"
MyForm(0).text1.text="Other Text"

In order to handle the form correctly I suggest you to have a control or a variable inside the original form the will be assigned with the index (in order to know exactly wich of the instance fired an event)

MyForm(0).labelindex.caption=0


Hope this help,

Max
0
 
dbrckoviAuthor Commented:
Thanx for replies!

Crazy_King_In

Your example works, but form J is not empty, it contains a Command Button.

I want to create a realy empty form, that would act exactly if it was created by 'Add New Form' from Design Mode.

I managed to simulate this by adding an empty form to my project (ftmTemplate), and then creating new forms as frmTemplate.   >>  Dim frmNewForm As New frmTemplate <<

Is there some more elegant way?

I mean, does it have to be created by a model (Form1)? can't it be created from scratch?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now