Results 1 to 10 of 10

Thread: easy way to check if a form is already open

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    VB Code:
    1. 'Class member variable
    2.     Dim frm2 As New Form2
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         If Not frm2 Is Nothing Then
    6.             frm2.Show()
    7.             frm2.Activate()
    8.         End If
    9.     End Sub

  3. #3
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    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:
    1. If Not myForm Is Nothing Then
    2.         'Check if the form is still considered created
    3.         If myForm.Created = True Then
    4.                  myForm.Show()
    5.                  myForm.Activate
    6.         End If
    7. End If
    Last edited by Carnifex; Feb 25th, 2004 at 05:42 PM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20
    Awesome. That looks just like the ticket.

    I will give it a shot
    Thanks
    Tony

  5. #5
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    The other option would be to set myForm to nothing in a form closed event.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20
    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

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Pirate
    VB Code:
    1. 'Class member variable
    2.     Dim frm2 As New Form2
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object,
    5. ByVal e As System.EventArgs) Handles Button1.Click
    6.         If Not frm2 Is Nothing Then
    7.             frm2.Show()
    8.             frm2.Activate()
    9.         End If
    10.     End Sub
    Did you try this way . It works fine for me .

  8. #8
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    If you want a new form to be created if it isn't then modify the code to this.

    VB Code:
    1. If Not myForm Is Nothing Then
    2.         'Check if the form is still considered created
    3.         If myForm.Created = True Then
    4.                  myForm.Show()
    5.                  myForm.Activate
    6.         Else
    7.                  myForm = New myForm
    8.                  myForm.Show
    9.         End If
    10. Else
    11.         myForm = New myForm
    12.         myForm.Show
    13. End If

    Basically creates a new instance of the form if the old one doesn't exist anymore/was never created.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    20
    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

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Pirate

    VB Code:
    1. 'Class member variable
    2.     Dim frm2 As New Form2
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object,
    5. ByVal e As System.EventArgs) Handles Button1.Click
    6.         If Not frm2 Is Nothing Then
    7.             frm2.Show()
    8.             frm2.Activate()
    9.         End If
    10.     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
  •  



Click Here to Expand Forum to Full Width