Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Found a Way To Close All Loaded Forms!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [RESOLVED] [2005] Found a Way To Close All Loaded Forms!

    I think whe you use My.Application.OpenForms, one can close all the applications' loaded forms but i am not getting it 100% right. Any Idea what i am getting wrong!
    VB Code:
    1. Private Sub closeAllLoadedForms()
    2.         For Each f As Form In My.Application.OpenForms
    3.             'If Not f.InvokeRequired Then
    4.             'Can access the form directlt
    5.             If Not f.Name = "frmPaneled" Then
    6.                 f.Close()
    7.                 f.Dispose()
    8.             End If
    9.             'End If
    10.         Next f
    11.     End Sub

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: [2005] Found a Way To Close All Loaded Forms!

    Once you close F and dispose of it, it's no longer in the OpenForms collection.
    Thus, the collection is changed during your loop, and that's probably what's causing your problem. When you remove an object from a collection, all the other objects comming after is assigned new index numbers.

    You actually have to loop backwards through the collection.
    VB Code:
    1. Private Sub closeAllLoadedForms()
    2.         Dim I As Integer
    3.         For I=My.Application.OpenForms.Count-1 to 0 Step -1
    4.             f=My.Application.OpenForms(I)
    5.             'If Not f.InvokeRequired Then
    6.             'Can access the form directlt
    7.             If Not f.Name = "frmPaneled" Then
    8.                 f.Close()
    9.                 f.Dispose()
    10.             End If
    11.             'End If
    12.         Next f
    13.     End Sub

    This way, the loop will still work even if a form is removed from the collection.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Found a Way To Close All Loaded Forms!

    Thanks pax. This will work too.
    VB Code:
    1. For i As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
    2.             If My.Application.OpenForms.Item(i) IsNot Me Then
    3.                 My.Application.OpenForms.Item(i).Close()
    4.             End If
    5.         Next i

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Found a Way To Close All Loaded Forms!

    But when i try to dispose them after closing them.
    My.Application.OpenForms.Item(i).Dispose()

    i get an exception!
    "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index"

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: [RESOLVED] [2005] Found a Way To Close All Loaded Forms!

    Thats because you're using My.Application.OpenForms(I) to both close it AND dispose it.
    Once you close it, it no longer exists in the collection, so My.Application.OpenForms(I) wont work if it's the last form in the collection.
    So you still have to store the form in a variable like F and then use f.Close and f.Dispose.

    Let's say that you have 3 form (index:0,1,2)
    Then you close Index 2
    Then you're left with index 0 and 1
    Then you try to dispose index 2.
    This fails, since you only have 0 and 1 left in the collection.
    That's why you need to store it in a variable.

    VB Code:
    1. Dim F As Form
    2. For i As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
    3.             F=My.Application.OpenForms.Item(i)
    4.             If F IsNot Me Then
    5.                 F.Close()
    6.                 F.Dispose()
    7.             End If
    8. Next i
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] [2005] Found a Way To Close All Loaded Forms!

    Correct.....My First Thumbs up to VS 2005

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