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
Printable View
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
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
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
Awesome. That looks just like the ticket.
I will give it a shot
Thanks
Tony
The other option would be to set myForm to nothing in a form closed event.
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
Did you try this way . It works fine for me .Quote:
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 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.
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
If you read carefully , you'd have solved it (class member variable) ;)Quote:
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