-
Dec 10th, 2019, 11:15 AM
#1
Thread Starter
Enjoy the moment
[RESOLVED] Vs 2017 check if form is open and close it
hey
i am trying to check a certain form if its open
and dispose it and reopen same form again
i tried a code
dosnt seem to do the job and the form dosnt close
this is my code
Code:
Dim isfound As Boolean = False
If isfound = False Then
If s.StartsWith("NMBR=") Then
isfound = True
If Application.OpenForms().OfType(Of FrmCallerID).Any Then
FrmCallerID.Close()
Dim frm As New FrmCallerID With {
.PhoneCallerID = s.Substring("NMBR=".Length)
}
frm.Show()
Else
Dim frm As New FrmCallerID With {
.PhoneCallerID = s.Substring("NMBR=".Length)
}
frm.Show()
End If
End If
End If
tnx for any help
salsa
-
Dec 10th, 2019, 11:40 AM
#2
Re: Vs 2017 check if form is open and close it
Is the form your startup form? I ask because I prefer not to handle this kind of problem in this fashion.
I have a couple cases where one form might open another form non-modally. In some cases it will just be one form opening one other instance of another form, while in other cases the first form will allow any number of instances of the other form to open. In both cases, though, some form (or class) is where the code to open the other forms resides. In that case, you already have (or had) some reference to the form that is opened, even if it is opened non-modally. If I have one form opening one other, then I just have the new form be a member variable of the form that opened it. If the one form is allowing any number of instances of the other form to open, then I keep a List of the other form, and for each one I open, I add it to the list.
By doing this, one form always knows about the other form, and a few different options are allowed. For one thing, you can handle the Closing events of the other forms from the form that showed them. If the Closing event is raised, you just remove that instance from the form that showed it. There's never a question of whether or not the form exists, at that point, because either you have it or you don't have it.
For example, if you keep the second form as a form-level variable of the first form, and handle the Closing event of the second form, then in the closing event of the second form, you set the form level variable to Nothing. Therefore, if the form level variable is Nothing, the second form is not shown. If the form level variable is not nothing, then the second form is shown, and you have access to it through that variable, so you can close it from the first form...and make another one immediately afterwards, if that's what you want to do.
My usual boring signature: Nothing
-
Dec 10th, 2019, 11:48 AM
#3
Thread Starter
Enjoy the moment
Re: Vs 2017 check if form is open and close it
hey shaggy its not startup form
-
Dec 10th, 2019, 12:46 PM
#4
Re: Vs 2017 check if form is open and close it
Here is something I use. Pass the form name to the function:
Code:
Private Function TheFormIsAlreadyLoaded(ByVal pFormName As String) As Boolean
TheFormIsAlreadyLoaded = False
For Each frm As Form In Application.OpenForms
If frm.Name.Equals(pFormName) Then
TheFormIsAlreadyLoaded = True
Exit Function
End If
Next
End Function
Please remember next time...elections matter!
-
Dec 10th, 2019, 12:48 PM
#5
Thread Starter
Enjoy the moment
Re: Vs 2017 check if form is open and close it
tnx tyson
but if the form is already loaded i need to close it and re-open it
how do i do that?
see my code #POST1
-
Dec 10th, 2019, 01:04 PM
#6
Re: Vs 2017 check if form is open and close it
Originally Posted by salsa31
tnx tyson
but if the form is already loaded i need to close it and re-open it
how do i do that?
see my code #POST1
Sorry...I missed that part.
Please remember next time...elections matter!
-
Dec 10th, 2019, 01:52 PM
#7
Re: Vs 2017 check if form is open and close it
It looks like you are closing the default instance of the form:
Code:
FrmCallerID.Close()
If the form you are looking at is indeed the default instance, then that is fine. However, if you've already executed the rest of the code:
Code:
Dim frm As New FrmCallerID With {
PhoneCallerID = s.Substring("NMBR=".Length)
}
frm.Show()
then you should be closing the form referenced by the frm variable. I think this is what Shaggy was telling you in his post above; you need to maintain a reference to the new form you create, and call close on that reference at the appropriate time.
-
Dec 10th, 2019, 01:55 PM
#8
Thread Starter
Enjoy the moment
Re: Vs 2017 check if form is open and close it
Originally Posted by Inferrd
It looks like you are closing the default instance of the form:
Code:
FrmCallerID.Close()
If the form you are looking at is indeed the default instance, then that is fine. However, if you've already executed the rest of the code:
Code:
Dim frm As New FrmCallerID With {
PhoneCallerID = s.Substring("NMBR=".Length)
}
frm.Show()
then you should be closing the form referenced by the frm variable. I think this is what Shaggy was telling you in his post above; you need to maintain a reference to the new form you create, and call close on that reference at the appropriate time.
sry friend i got lost a little
i am checking with this code if its open
Code:
If Application.OpenForms().OfType(Of FrmCallerID).Any Then
then closing if it is open
and the reopening the same form
FrmCallerID.Close()
-
Dec 10th, 2019, 02:07 PM
#9
Re: Vs 2017 check if form is open and close it
Originally Posted by salsa31
sry friend i got lost a little
i am checking with this code if its open
Code:
If Application.OpenForms().OfType(Of FrmCallerID).Any Then
then closing if it is open
and the reopening the same form
FrmCallerID.Close()
Are you closing then opening the form more than once in a session? In your code above, you are explicitly creating and opening a new form with
Code:
Dim frm As New FrmCallerID With {PhoneCallerID = s.Substring("NMBR=".Length)}
frm.Show()
How did you show the CallerID form for the first time? Did you do something similar to the code above (explicitly create an instance of the form), or did you just call FrmCallerID.Show? If you explicitly create a new instance of the form (Dim frm As New FrmCallerID) then calling frmCallerID.Close() isn't going to close it. frmCallerID is the default instance of the form, not a form you create yourself.
-
Dec 10th, 2019, 05:21 PM
#10
Re: Vs 2017 check if form is open and close it
What I was saying was that in this line:
Code:
Dim frm As New FrmCallerID With {PhoneCallerID = s.Substring("NMBR=".Length)}
rather than making frm a local variable, make it a form level variable. That way, you have access to frm from all parts of the form. frm would start out as Nothing, then when you create the form it would not be Nothing. So, if it isn't Nothing, then you close it. However, just closing it won't be enough, as you will also have to set frm = Nothing, or else it will hang onto that form longer than you want it to. Of course, if you will be creating a new one as soon as you close the one you have, then if frm IsNot Nothing, you'd dispose it, then create the new one and show it, so you'd never have to set frm to Nothing. You'd only set it to Nothing if the code to create the new one didn't come right after the code that disposed of the old one.
Also, I hadn't noticed it, but Inferrd is certainly correct. frmCallerID.Close closes only the default instance, which is not the one you created and showed. So, that will be closing a different form than the one you want.
My usual boring signature: Nothing
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
|