Open a form - simple Not!


Author
Message
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
No problem Smile  I just typed this out in the post and did not bring it in from code, so if you have an issue with what I posted let me know.  But it should at least get you most of the way there. Smile
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
This is why StrataFrame ROCKS!



Thanks Trent! This really (finally) clears up what you've mentioned a bunch of times in different posts about how to handle opening forms in an Application. I really appreciate it.
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
You can do that, but you first need to have a centralized collection that can be referenced.  You are actually better off doing this outside of the instantiaion for one primary reason...speed and maintainability...OK, that is two reasons Smile  We created a shared class called MenuSystem which houses all of our instantiation logic.  Security, form collections, multiple-instances, and whether the form will appear in the MDI window or on its own, all within one central location.  Ours had to be a bit more specialized since it can launch VFP forms or .NET forms.  But the logic is basically the same.

You could even create a class that is passed to the MenuSystem that has all of the logic that you need that you could pass over....or just create a bunch of overloads to do this for you.  It is just up to you, but the key is creating a class that launches all of your forms according to your environment and needs.

Public Notineritable Class MenuSystem

Private Shared _MenuMdiClient As MDIClient
Private Shared _Forms As New System.Collections.Generics.List(Of SYstem.Windows.Forms.Form)

'-- Reference to the MDI client to which all client forms will be added.  Be sure to set this in your
'    New of the parent that houses the MDI client.
Public SHared Property MenuMdiClient As MDIClient
    Get
        Return _MenuMdiClient
    End Get
    Set(Byval value as MdiClient)
        _MenuMdiClient = value
    End Set
End Property

'-- This collection contains all of the forms that are loaded
Public Shared Readonly Property Forms As System.Collections.Generics.List(Of System.Windows.Forms.Form)
    Get
         Return _Forms
    End Get
End Property

'-- Central method to load all of the
Public Shared Sub LaunchForm(ByVal FormType As System.Type, Byval SecurityKey As String, Byval SingleInstance As Boolean)
    '-- Establish Locals
    Dim loForm As System.WIndows.Forms.Form

    If SecurityBasics.CurrentUser.GetPermission(SecurityKey).Action = Grant Then
       '-- Check for a single instance
       If SingleInstance AndAlso FormIsRunning(FormType) Then
            Exit Sub
       End If

       '-- Create and launch the form
        loForm = CType(Activator.CreateInstance(FormType), System.WIndows.Forms.Form)
        loForm.MdiParent = _MenuMdiClient

        '-- Create a handler so it can remove itself from the collection
        AddHandler loForm.FormClosed, Addressof HandleFormClosed

        loForm.Show()
    End If

   Private Shared Function FormIsRunning(Byval FormType As System.Type) As Boolean
     Dim llReturn As Boolean = False

      For Each loForm As Form In _Forms
         If loForm.GetType() Is FormType Then
             llReturn = True
             Exit For
         End If
      Next

      Return llReturn
    End Function

    Private Shared Sub HandleFormClosed(Byval sender as Object, Byval e as System.EventArgs)
         _Forms.Remove(CType(sender, Form))
    End Sub

End Class

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Would it be possible to put some logic into the form's themselves, to see if an instance is already running?



I.e. before the InitializeComponents is called in the constructor, check if the form is already instantiated. Maybe use a shared property to note if the form has been instantiated yet (and hold a reference to the instantiated form). Or maybe use a shared factory method to create the form instead of using New.



Not sure if this is feasible or if there is any way to cancel the instantiation of a form and instead return a reference to an existing instance, but I seem to recall reading about the Singleton pattern and doing it something like this.
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
If you are running your forms through an MDI environment then you can just reference the MDIChildren collection and create a method to tell you if it is running.  We have a MenuSystem class that we pass all of our forms through and then create handlers so that the form will remove itself from our own collection.  Either way will work.  Below is a sample of how to reference the MDIChildren collection to see if a form is within the MDI collection.

Private Function IsFormRunning(ByVal FormType As System.Type) As Boolean
        '-- Establish Locals
        Dim llReturn As Boolean = False
        '-- Cycle through all of the children forms within this MDI
        For Each loForm As Form In Me.MdiChildren
            llReturn = loForm.GetType() Is FormType
            If llReturn Then Exit For
        Next
        Return llReturn
    End Function

 


Peter Jones
Peter Jones
Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)
Group: Forum Members
Posts: 386, Visits: 2.1K
Hi Guys,

A follow up - I want to check if a form is already open before I instasiate a new one. I know (thought I knew) how to do this with (and other variations on this theme):       

        Dim OpenForms As FormCollection
        OpenForms = Application.OpenForms

        ' Lets see if the form is already open and, if it is, bring it to the front.
        For i As Integer = 0 To System.Windows.Forms.Application.OpenForms.Count - 1
            If OpenForms(i).Tag.ToString = e.Link.Item.Name.ToString Then
                OpenForms(i).WindowState = FormWindowState.Normal
                OpenTheForm = False
                Exit For
            End If
        Next

However, OpenForms.Count always returns zero (even the first time in when just the menu form itself if running). I've been Googling madly to see if there is any reference to the OpenForms.Count being zero and I couldn't find anything - lots of reference to the property in examples of working code - no reference to any problems/caveats.

Any ideas?

Cheers, Peter

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Thanks Trent. Slowly its starting to make sense.... Alien
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
The Activator accepts a Param Array as a second parameter which allows you to provide parms to the constructor of a created instance:

Activator.CreateInstance(GetType(MyForm), New Object() {Parm1})

or

Activator.CreateInstance(GetType(MyForm), Parm1)

Peter Jones
Peter Jones
Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)Advanced StrataFrame User (504 reputation)
Group: Forum Members
Posts: 386, Visits: 2.1K
Hi Guys,

Thanks for your input and Ben, thanks for taken the time to explain what was happening in that line of code - very helpful.

All in all this is sooooo depressing - it seems that every time I learn a bit more about .Net it only serves to underline how little I know!!

Cheers, Peter

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Ben (Chase),



Could you post a little code sample that shows how to do this if you want to pass values to a constructor of the form? This topic is in the category of "I get it until I have to code it...then I'm confused" Blink
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search