|
-
Oct 11th, 2006, 09:54 AM
#1
Thread Starter
Hyperactive Member
[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:
Private Sub closeAllLoadedForms()
For Each f As Form In My.Application.OpenForms
'If Not f.InvokeRequired Then
'Can access the form directlt
If Not f.Name = "frmPaneled" Then
f.Close()
f.Dispose()
End If
'End If
Next f
End Sub
-
Oct 11th, 2006, 10:04 AM
#2
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:
Private Sub closeAllLoadedForms()
Dim I As Integer
For I=My.Application.OpenForms.Count-1 to 0 Step -1
f=My.Application.OpenForms(I)
'If Not f.InvokeRequired Then
'Can access the form directlt
If Not f.Name = "frmPaneled" Then
f.Close()
f.Dispose()
End If
'End If
Next f
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...
-
Oct 11th, 2006, 10:09 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Found a Way To Close All Loaded Forms!
Thanks pax. This will work too.
VB Code:
For i As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
If My.Application.OpenForms.Item(i) IsNot Me Then
My.Application.OpenForms.Item(i).Close()
End If
Next i
-
Oct 11th, 2006, 10:13 AM
#4
Thread Starter
Hyperactive Member
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"
-
Oct 11th, 2006, 10:27 AM
#5
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:
Dim F As Form
For i As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
F=My.Application.OpenForms.Item(i)
If F IsNot Me Then
F.Close()
F.Dispose()
End If
Next i
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Oct 11th, 2006, 10:39 AM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|