Results 1 to 5 of 5

Thread: VB.NET: How to get the good old Forms Collection

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    VB.NET: How to get the good old Forms Collection

    Remember the good old Forms collection in VB 6.0. How easy it was to enumerate through the Forms that were loaded. In .NET we don't have any such collection.

    In .NET this can be achieved by adding a simple Class with a Shared member.
    VB Code:
    1. Public Class FormsCollection
    2.     Public Shared Forms As ArrayList = New ArrayList
    3. End Class

    Now in the constructor of all the Forms write
    VB Code:
    1. FormsCollection.Forms.Add(Me)
    This will add the Forms to the collection whenever they are created in the memory.

    Now to loop through the collection we can use
    VB Code:
    1. For Each f As Form In FormsCollection.Forms
    2.      'rest of code here            
    3. Next

    Remember when you close the Form you need to remove the Form from the Collection using
    VB Code:
    1. FormsCollection.Forms.Remove(Me)
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  2. #2

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: VB.NET: How to get the good old Forms Collection

    The above post is good for .NET 1.1 or earlier. VB 2005 has got the Forms collection again.

    Microsoft has retained lot of functionality from good old VB 6 in VB 2005. Now you do not have to write code for having a Forms Collection, MY object in VB 2005 has got it for you. My.Application.OpenForms property can be used to iterate through all loaded and visible forms in the current application.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    New Member
    Join Date
    Jun 2006
    Posts
    1

    Re: VB.NET: How to get the good old Forms Collection

    how to do this in visual c++ .net 2003?
    i'm kinda new to vc++ and i have this impression that whatever can be done in vb.net can also be done in vc++.net.

    do i have to put the line
    VB Code:
    1. FormsCollection.Forms.Add(Me)
    in the Load event of the Form?

    also, how do i make a class in vc++ with a shared member like the one you listed above? (this is probably not the right place to ask this question but someone who's knowledgeable in vc++ and vb might stumble upon this thread).

    i hope someone there's someone out there who could help me. this is the closest i got to finding a solution to looping through opened forms in .NET 2003, and it's in VB.

  4. #4

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: VB.NET: How to get the good old Forms Collection

    I don't think I'll be of any help here. I don't have any experience in VC++.

    You can probably ask this question on Codeguru
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET: How to get the good old Forms Collection

    The C++ and C# equivalents of 'Me' and 'Shared' are 'this' and 'static'.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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