|
-
Feb 25th, 2004, 02:43 PM
#1
Thread Starter
Junior Member
easy way to check if a form is already open
Is there any way to check if a form is already open and if so set the focus to it instead of opening another instance of that form ?
Thanks
--
Tony
-
Feb 25th, 2004, 03:31 PM
#2
Sleep mode
VB Code:
'Class member variable
Dim frm2 As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not frm2 Is Nothing Then
frm2.Show()
frm2.Activate()
End If
End Sub
-
Feb 25th, 2004, 05:37 PM
#3
Addicted Member
You also need to check if the form has been closed (disposed) as it will not be nothing, and will give you an object disposed exception if you try to open it.
Heres an edit to Pirates code.
VB Code:
If Not myForm Is Nothing Then
'Check if the form is still considered created
If myForm.Created = True Then
myForm.Show()
myForm.Activate
End If
End If
Last edited by Carnifex; Feb 25th, 2004 at 05:42 PM.
-
Feb 25th, 2004, 05:38 PM
#4
Thread Starter
Junior Member
Awesome. That looks just like the ticket.
I will give it a shot
Thanks
Tony
-
Feb 25th, 2004, 05:44 PM
#5
Addicted Member
The other option would be to set myForm to nothing in a form closed event.
-
Feb 26th, 2004, 02:27 PM
#6
Thread Starter
Junior Member
I tried that and when I click on the button to open my new form, it doesn't do anything.
My form is called
Here is the code behind the click event of the button to open my form:
Dim AddNewShoppingForm As New frmAddNewShopping
If AddNewShoppingForm Is Nothing Then
If AddNewShoppingForm.Created = True Then
AddNewShoppingForm.Show()
AddNewShoppingForm.Activate()
End If
End If
-
Feb 26th, 2004, 04:30 PM
#7
Sleep mode
Originally posted by Pirate
VB Code:
'Class member variable
Dim frm2 As New Form2
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
If Not frm2 Is Nothing Then
frm2.Show()
frm2.Activate()
End If
End Sub
Did you try this way . It works fine for me .
-
Feb 26th, 2004, 06:02 PM
#8
Addicted Member
If you want a new form to be created if it isn't then modify the code to this.
VB Code:
If Not myForm Is Nothing Then
'Check if the form is still considered created
If myForm.Created = True Then
myForm.Show()
myForm.Activate
Else
myForm = New myForm
myForm.Show
End If
Else
myForm = New myForm
myForm.Show
End If
Basically creates a new instance of the form if the old one doesn't exist anymore/was never created.
-
Feb 27th, 2004, 04:30 PM
#9
Thread Starter
Junior Member
Ah that actually works.
I found the problem. I had to move the dim statement for the new form to the declatations and it now works fine.
I had the dim statement before in the click event of the button and it didn't work there.
Thanks all
Tony
-
Feb 27th, 2004, 09:50 PM
#10
Sleep mode
Originally posted by Pirate
VB Code:
'Class member variable
Dim frm2 As New Form2
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
If Not frm2 Is Nothing Then
frm2.Show()
frm2.Activate()
End If
End Sub
If you read carefully , you'd have solved it (class member variable)
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
|