Multi Instance Forms

This code will show you how to use forms in multi instances, meaning you can create many instances of a control at runtime but yet only need create the form once at design. To use this code you need:

2 Forms: - frmLaunch and frmMain
1 command Button on frmlaunch: - cmdNew
1 lable on frmMain: - lblNum

Paste the following code into frmLaunch:

Option Explicit
'//Define Variables for the form
Dim NewPage() As New frmMain

'//Define Variables for the Number of forms that have been opened
Public Pages As Integer

Private Sub cmdNew_Click()
    'add one to the number of forms
    Pages = Pages + 1
    ReDim Preserve NewPage(Pages)
    'set the new forms page number
    NewPage(Pages).lblNum.Caption = Pages
    'open a new page
    NewPage(Pages).Show
    'redimensionise the new page array
    ReDim NewPage(Pages + 1) As New frmMain
End Sub

You might also like...

Comments

Dominic Miller

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”