|
-
Jan 20th, 2010, 02:09 AM
#1
Thread Starter
Addicted Member
Check if all forms of an application has been closed/disposed
How do you check if all instances of forms (or simply all forms of an application) has already been closed?
====================
ほんとにどもありがとう!
Rie Ishida
-
Jan 20th, 2010, 02:12 AM
#2
Re: Check if all forms of an application has been closed/disposed
Normally, the program would exit at this point.
If you chose not to use the application framework, you'd have to check it yourself.
It is generally considered a good practice to keep track of all your forms (and other objects).
If all references to your forms are null then all of them have been disposed.
-
Jan 20th, 2010, 02:20 AM
#3
Thread Starter
Addicted Member
Re: Check if all forms of an application has been closed/disposed
You see, I have an MDIform, and there are instances when a child form "goes out" of the MDIform even if I declared that form to be a child form of the MDIform (I'm using the singleton method in opening instances of forms, so that I can limit each form's instance count to one). I want to know how to check if any other form other than the MDI form is still running so that before I close the MDIform, it would first check if there are any other forms running other than the MDIform, and will ask the user to close those forms first.
Or if there is some other way to close all the forms right away (like End, I guess).
====================
ほんとにどもありがとう!
Rie Ishida
-
Jan 20th, 2010, 02:28 AM
#4
Re: Check if all forms of an application has been closed/disposed
It is not advisable, but yes, you can use End (or Environment.Exit).
What do you mean by 'singleton' method? And how a child form 'goes out'?
If you keep a reference to your form you can always control its behavior. I don't understand why couldn't you just check whether they are null?
-
Jan 20th, 2010, 02:32 AM
#5
Thread Starter
Addicted Member
Re: Check if all forms of an application has been closed/disposed
 Originally Posted by cicatrix
What do you mean by 'singleton' method? And how a child form 'goes out'?
If you keep a reference to your form you can always control its behavior.
Like this... I place the following property in the designer code of the form (eg: frmBorrowingForm):
Code:
Private Shared _instanceBorrowingForm As frmBorrowingForm
Public Shared ReadOnly Property Instance() As frmBorrowingForm
Get
If _instanceBorrowingForm Is Nothing OrElse _instanceBorrowingForm.IsDisposed Then
'There is no current instance so create one.
_instanceBorrowingForm = New frmBorrowingForm
End If
'Return a reference to the one and only instance.
Return _instanceBorrowingForm
End Get
End Property
And call its instance using this code:
Code:
frmBorrowing.instance.show()
frmBorrowing.instance.activate()
 Originally Posted by cicatrix
If you keep a reference to your form you can always control its behavior. I don't understand why couldn't you just check whether they are null?
How do I do this?
====================
ほんとにどもありがとう!
Rie Ishida
-
Jan 20th, 2010, 02:42 AM
#6
Re: Check if all forms of an application has been closed/disposed
If you want to close the application then call Application.Exit.
-
Jan 20th, 2010, 03:01 AM
#7
Thread Starter
Addicted Member
Re: Check if all forms of an application has been closed/disposed
 Originally Posted by jmcilhinney
If you want to close the application then call Application.Exit.
I would gladly do that, but after the MDIform is closed, I would have to show the hidden frmLogin, that's why I can't just call Application.Exit.
====================
ほんとにどもありがとう!
Rie Ishida
-
Jan 20th, 2010, 03:08 AM
#8
Re: Check if all forms of an application has been closed/disposed
 Originally Posted by riechan
I would gladly do that, but after the MDIform is closed, I would have to show the hidden frmLogin, that's why I can't just call Application.Exit.
I'm not sure what you're saying. You suggest End as a possible solution, which is evil, yet Application.Exit is no good. That doesn't seem to make sense.
Also, you really shouldn't have a hidden login form. Display the login form when the user is logging in and then close it. If the user logs out and you want someone else to login then create a new login form and then close it when you're done with it too.
-
Jan 20th, 2010, 03:15 AM
#9
Re: Check if all forms of an application has been closed/disposed
 Originally Posted by riechan
Like this... I place the following property in the designer code of the form (eg: frmBorrowingForm):
Code:
Private Shared _instanceBorrowingForm As frmBorrowingForm
Public Shared ReadOnly Property Instance() As frmBorrowingForm
Get
If _instanceBorrowingForm Is Nothing OrElse _instanceBorrowingForm.IsDisposed Then
'There is no current instance so create one.
_instanceBorrowingForm = New frmBorrowingForm
End If
'Return a reference to the one and only instance.
Return _instanceBorrowingForm
End Get
End Property
And call its instance using this code:
Code:
frmBorrowing.instance.show()
frmBorrowing.instance.activate()
I see, then I suggest to move further from that (even though I don't undestand why don't you do it the normal way) - incorporate instance counters in forms and probaly a method which will dispose of all instances if needed.
Normally I load a form like this:
Code:
Dim frm As New Form1
frm.Show
This way I can control my form by using the frm reference. If I call frm.Dispose method the form will cease to exist.
If I have many forms then I create a collection of forms and can easily acces to any of my forms.
Your approach strips you of the opportunity to know exactly how many forms are opened.
@jmcilhinney - are you aware of the fact that Application.Exit does not necessarily lead to the end of application (just a side thought).
Environment.Exit(0) will always end the application.
-
Jan 20th, 2010, 03:21 AM
#10
Thread Starter
Addicted Member
Re: Check if all forms of an application has been closed/disposed
@jm: I tried using Application.Exit, and got this error: Collection was modified; enumeration operation may not execute.
 Originally Posted by cicatrix
I see, then I suggest to move further from that (even though I don't undestand why don't you do it the normal way) - incorporate instance counters in forms and probaly a method which will dispose of all instances if needed.
Normally I load a form like this:
Code:
Dim frm As New Form1
frm.Show
This way I can control my form by using the frm reference. If I call frm.Dispose method the form will cease to exist.
If I have many forms then I create a collection of forms and can easily acces to any of my forms.
Your approach strips you of the opportunity to know exactly how many forms are opened.
I did load a couple of forms that way. If I declare this line with the public variables,
Code:
Dim frm As New Form1
it only opens one form, right? How do I count all the open forms of the application and how do I make the newly-opened form a child form of the MDIform (even after it was opened by a child form as well)?
====================
ほんとにどもありがとう!
Rie Ishida
-
Jan 20th, 2010, 03:29 AM
#11
Re: Check if all forms of an application has been closed/disposed
vb.net Code:
' Key can be of any type, I used string Public fc As New Collections.Generic.Dictionary(Of String, Form) ' If you don't want to use keys, you can use List collection instead fc.Add("myNewForm", New Form1) fc("myNewForm").Show() Debug.Print("Here are the loaded forms:") For Each KVPair In fc Debug.Print(String.Format("Key {0}, form name {1}", KVPair.Key, KVPair.Value.Name)) Next Debug.Print(String.Format("Total: {0} forms are loaded", fc.Count))
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
|