Solved

Creating new forms from code

Posted on 2003-11-11
3
199 Views
Last Modified: 2010-05-03
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
Comment
Question by:dbrckovi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
Crazy_king_in earned 35 total points
ID: 9721598
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
 
LVL 2

Expert Comment

by:MaxPol
ID: 9721764
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
 
LVL 11

Author Comment

by:dbrckovi
ID: 9731028
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question