Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 80
  • Last Modified:

Use Multiple Forms

Why first form visible after last command in :

Public frm() as New Form1
Sub Main
  Redim frm(0)

 frm(0). Show

 Redim frm(1)
 frm(1).Show
....
 Unload frm(1)
set frm(1)=Nothing

,,,
unload frm(0)
set frm(0)=Nothing

Debug.Print Forms.Count resut 1 , I beleave but must be zero, no ?
0
mancio
Asked:
mancio
  • 3
1 Solution
 
Martin LissRetired ProgrammerCommented:
Try it like this.
'Public frm As New Form1 'Note that this is commented out
Sub Main()

Dim f As Form
 ShowForm

 ShowForm

For Each f In Forms
    If f.Tag = "1" Then
        Unload f
        Set f = Nothing
        Exit For
    End If
Next
For Each f In Forms
    If f.Tag = "2" Then
        Unload f
        Set f = Nothing
        Exit For
    End If
Next


Debug.Print Forms.Count ' resut 1 , I beleave but must be zero, no ?
End Sub
Sub ShowForm()
Dim frm As New Form1
frm.Show
frm.Tag = Forms.Count
End Sub

Open in new window

0
 
Martin LissRetired ProgrammerCommented:
I just realized what the problem is with your code. You were Redim-ing without using the Preserve keyword. That caused the form array to be rebuilt rather than just added to. This works. See line 7.

Public frm() As New Form1
Sub Main()
  ReDim frm(0)

 frm(0).Show

 ReDim Preserve frm(1)
 frm(1).Show

 Unload frm(1)
Set frm(1) = Nothing

Unload frm(0)
Set frm(0) = Nothing

Debug.Print Forms.Count ' resut 1 , I beleave but must be zero, no ?
End Sub

Open in new window

0
 
mancioAuthor Commented:
Thanks Martin Liss,
0
 
Martin LissRetired ProgrammerCommented:
Please don't forget to choose one of my solutions as the answer.

In any case, you're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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