Hi,
How I can close all the forms opened of my application except a form that I do not want to close?
regards
Printable View
Hi,
How I can close all the forms opened of my application except a form that I do not want to close?
regards
How are you creating/opening them? Since there is no forms collection in 2003 you will have to either close them manually, when tracking with public object variables, or when you close the parent form it will close the child form(s) automatically.
U can use an arraylist containning all your opened forms and then when needed search the array to close all the unwanted forms.
U can create a module and make your array public in there for all the forms
I'm using visual basic 2005
here is an example of what I said earlier
Yes, public object variables like I posted earlier will help you track what forms are open and give you the ability to close which one(s) you want.
Note, 2005 projects are not openable by 2003. You will have to use notepad or other text editor to just view the code and not able to run it.
Note also that closing your startup form will exit the application, so unless this one form you don't want to close is the startup form you're going to have to tap dance a bit to get it to work.
Thanks to all for answering.
There is no problem, also I have vb 2005.Quote:
Note, 2005 projects are not openable by 2003. You will have to use notepad or other text editor to just view the code and not able to run it.
Talkro, thank you for your contribution. I will check your test project and I try it.Quote:
here is an example of what I said earlier
regards
Now I try it and it runs good.
I am very glad for your contribution. I have implemented it in my application, modifying some aspect, and it works perfectly.
Thank you very much to all.
Regards
Sorry, I have a problem.
When I can remove form from arrayList?
Because Now I remove form from Array when form is disposed:
For example:
Code:Public Class frmEmpresas
#Region " Windows Form Designer generated code "
Private Shared frmEmpEj As frmEmpresas = Nothing
Public Shared Function Instance() As frmEmpresas
If frmEmpEj Is Nothing Then
frmEmpEj = New frmEmpresas
addForm(frmEmpEj)
End If
frmEmpEj.Focus()
Return frmEmpEj
End Function 'Instance
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
removeForm(frmEmpEj)
frmEmpEj = Nothing
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
.
.
.
End Class
But when my Main Form of the app (MDI parent form) executes this code:
Error: "Index out of the interval"Code:Dim i As Integer
For i = 0 To formArray.Count - 1
Dim f As Form = CType(formArray(i), Form)
f.Close()
Next
formArray.Clear()
regards,
please, any idea?
Regards
Have you tried using break points to see what the content of your formArray is?
At waht value of i does it gives you the error?
Yes, because if I remove form from Array, this have less items.
For example, If I add two forms in ArrayList, after when I executes this code:
When the form is closed:Code:Dim i As Integer
'array have two items
For i = 0 To formArray.Count - 1
Dim f As Form = CType(formArray(i), Form)
f.Close() 'but if close the form, this remove from ArrayList. Now ArrayList only have one item. For this it fails.
Next
formArray.Clear()
I hope that you have understood me, because my English is not very good.Code:removeForm(myForm)
Regards
be careful with a for loop on a dynamic size array, when the array is decreasing and i increasing, at some point i is greater than the size of the array and trying to access a value at that position in the array.
try this
vb Code:
while formArray.count >= 0 dim f as form = ctype(formArray(i), form) f.close() end while formArray.Clear()
Hi talkro,
I have tried this code but the same error continues displaying.
regards
Are you using this array to store MDI Child forms? (You mentioned above that the only form remaining open is the MDI parent) If so you don't need to use an array, you should use the "MdiChildren" collection of the MDI Parent form.
VB Code:
For Each frmCloseure As Form In Me.MdiChildren frmCloseure.Close() frmCloseure.Dispose() Next
I have seen where the failure originates.
I explain it.
How I want to open different instances of the same child form from other child form:
formChild1 (This one is a child form who opens other child forms)
Code:Sub openFrm(ByVal nombre As String, ByVal accion As String, Optional ByVal id As Integer = -1)
Try
For Each forma As Form In Me.ParentForm.MdiChildren
If forma.Text = nombre Then
forma.Focus()
Exit Sub
End If
Next
fNouEmpl = New frmNouEmpl
addForm(fNouEmpl)
fNouEmpl.Tag = Guid.NewGuid().ToString
mColFrm2s.Add(fNouEmpl, fNouEmpl.Tag)
AddHandler fNouEmpl.Closed, AddressOf fNouEmpl_Closed
fNouEmpl.MdiParent = Me.MdiParent
fNouEmpl.Text = nombre
If id <> -1 Then
fNouEmpl.idEmplM = id
End If
fNouEmpl.accEmpl = accion
fNouEmpl.Show()
Catch ex As Exception
cl.shErr(ex.Message, Me.Text)
cl.controlError(Me.Text, ex.ToString)
End Try
End Sub
In the case that fNouEmpl is closed:
If I close formChild1 before that fNouEmpl :Code:Private Sub fNouEmpl_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
fNouEmpl.Dispose()
Me.connGrid(condicio, condicio2)
removeForm(fNouEmpl)
End Sub
until here runs ok.Code:Private Sub frmEmpleados_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
For Each fNouEmpl In mColFrm2s
'Use this if you want to leave the frm2's open but don't want them to fire the event any more
RemoveHandler fNouEmpl.Closed, AddressOf fNouEmpl_Closed
Next
End Sub
The problem is if I close formChild1 before fNouEmpl instances, because when I close the instances of fNouEmpl, it will not call "removeForm".
I don't know how to solve this. Any idea?
Regards
Yes! it runs perfectly :)Quote:
Originally Posted by ProphetBeal
Thanks a lot.
Regards
I'm glad its working now :)
Hi,
I have simplified the code to put it here.
When I close fNouEmpl and formChil1d is not been closed , this one must execute a few functions.
In the case that formChild1 was closed before fNouEmpl, I must delete the event, And I did not have this code it would display error.
Sorry if I do not explain very well and because I cannot put here the all code.
regards