This little bit of code implements A true VB6 style Forms Collection. All you have to do to implement it is inherit from FormEx instead of System.Windows.Forms.Form and if you want your Form to be in the collection and thus be global through the static/shared FormsExCollection you pass a key to the constructor when you create your Form in the Sub Main or where evere set it equal to a new FormEx (New FormEx("FormsKey")) after thats you can access the Form from anywhere in code by simply typing FormEx.Forms("FormsKey").Member.Member()
This is a lite version of the one I am making in C# and there are some issues to be aware of like i don't do anything about the developer adding a key twice. And the collection only has limetted Collection behavior. The C# one is allot better because it is the vesion I am actually developing but I thought you guys might want to have a look at this and it might be useful to somebody even in the lite version. the C# version is in the C# forum if anyone wants to look it.
Oh! I have not done any tests with this one I just typed it out in the forum, so it might have a bug or two but I don't think it does the code is fairly simple the only thing I'm not sure about is the Item property because it's been a long time since I have coded in vb.net. Should work though.
VB Code:
Public Class FormEx Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() End Sub Public Sub New(ByVal key As String) MyBase.New() _formsex.Add(Me, key) End Sub Private Shared _formsex As FormsExCollection = New FormsExCollection() Public Shared Property Forms() As FormsExCollection Get Return _formsex End Get Set(ByVal Value As FormsExCollection) _formsex = Value End Set End Property Public Overloads Overrides Sub Dispose() If _formsex.Contains(Me) Then _formsex.Remove(Me) MyBase.Dispose(True) GC.SuppressFinalize(True) End Sub End Class Public Class FormsExCollection Private _keys As System.Collections.ArrayList = New System.Collections.ArrayList() Private _formsex As System.Collections.ArrayList = New System.Collections.ArrayList() Friend Function Add(ByRef formex As FormEx, ByVal key As String) As Integer _keys.Add(key) Return _formsex.Add(formex) End Function Friend Sub Remove(ByRef formex As FormEx) _keys.RemoveAt(_formsex.IndexOf(formex)) _formsex.Remove(formex) End Sub Public ReadOnly Property Count() As Integer Get Return _formsex.Count() End Get End Property Public Function Contains(ByRef formex As FormEx) As Boolean Return _formsex.Contains(formex) End Function Public Property Item(ByVal index As Integer) As FormEx Get Return CType(_formsex(index), FormEx) End Get Set(ByVal Value As FormEx) _formsex(index) = Value End Set End Property Public Property Item(ByVal key As String) As FormEx Get Return CType(_formsex(_keys.IndexOf(key)), FormEx) End Get Set(ByVal Value As FormEx) _formsex(_keys.IndexOf(key)) = Value End Set End Property End Class


Reply With Quote