Results 1 to 3 of 3

Thread: Forms collection - Can be used as a tutorial - SOLVED

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    New York
    Posts
    165

    Question Forms collection - Can be used as a tutorial - SOLVED

    Hi,

    I am creating an application which has an MDIForm as the startup form. From here, the user clicks on a 'Get Content' button, which calls the GetContent() procedure, and x amount of forms are created depending on how many documents are open in Word.

    For this example, we will say 3 documents are open in Word, therefore when the user clicks on the 'Get Content' button, 3 forms are created:

    '***********
    'Create forms:
    '***********
    Public Sub GetContent()
    Dim f1 as Form1
    Dim myFormCollection As New Form1Collection()

    'Remove all current forms:
    If myFormCollection.Count > 0 Then
    myFormCollection.Clear
    End If

    For i = 1 To intDocCount 'Number of Word docs. open
    f1 = New Form1() 'Create new Form
    f1.MdiParent = Me
    f1.Text = objWord.Documents.Item(i).FullName
    myFormCollection.Add(f1) 'Add the Form to the Collection class.
    Next
    End Sub

    To keep track of the forms, I am using the Form1Collection class which I created. Every time a Form is created it is added to the class: myFormCollection.Add(f1)

    '*********************
    'Class: Form1Collection
    '*********************
    Public Class Form1Collection
    Inherits System.Collections.CollectionBase

    Public Function Add(ByVal myForm1 As Form1)
    List.Add(myForm1)
    End Function

    End Class

    The problem I am having is with the section of GetContent() that removes all current forms. This is designed to protect the user if they click on the 'Get Content' button more than once. Instead of lots of forms building up on top of each other, this clears all current forms. However, even though I am using the .Clear method, the forms build up on top of each other. Ie. If 3 Word documents are open and the user keeps clicking 'Get Content', the forms increment in 3, 6 then 9 etc.

    I know this is a bit long, but as a complete newbie (like most of us to this, any help really would be much appreciated

    Any ideas?

    Thanks

    Chris
    Last edited by csf; Apr 12th, 2002 at 11:24 AM.

  2. #2
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    VB Code:
    1. If myFormCollection.Count > 0 Then
    2.    myFormCollection.Clear
    3. End If

    The first thing is that the code above merely removes your collection of form objects - it doesn't actually close down the forms held within.

    To do this you would need to do something like:-

    VB Code:
    1. Dim oForm as Form
    2. For each oForm in myFormCollection
    3.      oForm.Dispose
    4. Next
    5. myFormCollection.Clear

    However, in saying this you do not need to hold a collection of all your active forms as the mdi parents holds this already.

    For example,
    VB Code:
    1. '************************************************
    2.       '* Close down all open mdi children             *
    3.       '************************************************
    4.       Dim oForm As Form
    5.       Dim booOver As Boolean = False
    6.       Do Until booOver
    7.          oForm = Me.ActiveMdiChild
    8.          If oForm Is Nothing Then
    9.             booOver = True
    10.          Else
    11.             oForm.Dispose()
    12.          End If
    13.       Loop


    Hope this helps

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    New York
    Posts
    165
    Great, thanks! That worked perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width