Results 1 to 4 of 4

Thread: Check if Form is Open

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Check if Form is Open

    I have a Form that I reuse quite a bit. In the Form's New constructor, it deserializes a binary file and then loads data in the Form. For what it is worth, here is the code in the New constructor:
    Code:
    Sub New(ByVal path As String)
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Read the path argument to deserialize the sensor
        Using stream As IO.FileStream = IO.File.OpenRead(path)
            Try
                Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                sensor = DirectCast(formatter.Deserialize(stream), [removed].[removed].Sensor)
                stream.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Using
    End Sub
    One piece of data that it loads is the navigation, what this does is iterate through each FileInfo in the DirectoryInfo that matches the *.dat pattern to create a Button that displays the FileInfo's Name (minus the extension) and stores the FileInfo's FullName in the Tag. When the user clicks on the Button I want for it to either create a new Form passing the data from the Button's Tag or simply show the existing Form.

    What I need help with is the latter part of the condition that I described. How can I check if a Form has been created with the desired path? One option that I had was to simply store the path in the Form's Tag property, then iterating through each Form in the Application.OpenForms property, and then checking if the Form's Tag equals the desired path. But now I'm second guessing myself and wanted to come here to see if there was a better option.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Check if Form is Open

    To be precise, there's no such thing as a "New constructor". The New method is a constructor.

    Also, that code doesn't belong in the constructor. Reading data like that is a job for the Load event handler, not the constructor.

    As for he question, I would tend to use a Dictionary to store the forms, either by path or by Button, e.g.
    vb.net Code:
    1. Private formsByFilePath As New Dictionary(Of String, Form)
    2.  
    3. Private Sub ShowAndActivateForm(filePath As String)
    4.     Dim frm As Form
    5.  
    6.     If Not formsByFilePath.TryGetValue(filePath, frm) OrElse frm.IsDisposed Then
    7.         frm = New Form()
    8.         formsByFilePath.Add(filePath, frm)
    9.     End If
    10.  
    11.     frm.Show()
    12.     frm.Activate()
    13. End Sub
    Those last two lines will never both have an effect. If the form is already displayed then Show will have no effect and Activate will ensure that it has focus while if it's a new form the Show will display it and Activate will have no effect because it will already have focus.

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Check if Form is Open

    Could you elaborate on why the code would not belong in the constructor? To elaborate on why I currently have it as such, I also have a constructor without any parameters that gets the first .dat file in a default folder (but does the same thing). My reasoning for creating the two constructors (one with the path parameter and one without) was so that when the application loaded, it would create the Form using the first file and then when the user clicked on the button in the navigation it would create the Form using desired path.

    Also, how would I update the Dictionary on all of the other Forms? My first guess would be to make the variable Public, then when the user clicked on the Button it would iterate through each Form in the Application.OpenForms property, and then add the path/Form to that Form's respective Dictionary. My second guess would be to just make a "global" variable that would reside somewhere and then when the user clicked on the Button it would add the path/Form to that global Dictionary. Is there another, more effective way, or either of the two would work just fine?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Check if Form is Open

    Quote Originally Posted by dday9 View Post
    Could you elaborate on why the code would not belong in the constructor?
    I can't recall exactly where I read it but it was on MSDN somewhere. It said that a form constructor should not contain code that can potentially run for a lengthy period or could throw an exception. File I/O is disqualified on both counts.
    Quote Originally Posted by dday9 View Post
    Also, how would I update the Dictionary on all of the other Forms?
    You wouldn't. There's no need to. You're only opening those forms from one place, right? That place is the only place that needs to know that the Dictionary exists.

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