|
-
Feb 5th, 2004, 02:07 PM
#1
Thread Starter
Frenzied Member
VB6 Forms Collection
This is My work around for wanting a Forms collection. you inherit from the FormEx instead of Form and if you pass a key to the the ForEx Constructor it is added to the FormEx.Forms Collection......
I saw the collection question below and thought some of you guys might want this. the FormEx is in dev right now actualy provides a set of extended events properties and methods for skining, zooming, wiping, and what not but it's under dev right now so nothing is included here but the collection.
FormEx behaves Exactly like any other form except for providing the Forms Collection.
The Collection
Code:
using System;
namespace System.Windows.Forms
{
/// <summary>
/// Static collection of all System.Windows.Forms.FormEx objects.
/// </summary>
public sealed class FormExCollection : System.Collections.ICollection
{
public FormExCollection(){}
private System.Collections.ArrayList _keys = new System.Collections.ArrayList();
private System.Collections.ArrayList _forms = new System.Collections.ArrayList();
#region Internal Members
internal int Add(System.Windows.Forms.FormEx form, string key)
{
_keys.Add(key);
_forms.Add(form);
return (_forms.Count-1);
}
internal void Insert(int index, System.Windows.Forms.FormEx form, string key)
{
_keys.Insert(index, key);
_forms.Insert(index, key);
}
internal void Remove(System.Windows.Forms.FormEx form)
{
_keys.RemoveAt(_forms.IndexOf(form));
_forms.Remove(form);
}
internal void RemoveAt(int index)
{
_keys.RemoveAt(index);
_forms.RemoveAt(index);
}
#endregion
#region Public Members
public System.Windows.Forms.FormEx this[int index]
{
get{return (System.Windows.Forms.FormEx)_forms[index];}
set{_forms[index] = value;}
}
public System.Windows.Forms.FormEx this[string key]
{
get{return (System.Windows.Forms.FormEx)_forms[_keys.IndexOf(key)];}
set{_forms[_keys.IndexOf(key)] = value;}
}
public bool Contains(System.Windows.Forms.FormEx form)
{
return _forms.Contains(form);
}
public void CopyTo(System.Array array, int index)
{
_forms.CopyTo(array, index);
}
public int Count
{
get{return _forms.Count;}
}
public System.Collections.IEnumerator GetEnumerator()
{
return _forms.GetEnumerator();
}
public int IndexOf(string key)
{
return _keys.IndexOf(key);
}
public int IndexOf(System.Windows.Forms.FormEx form)
{
return _forms.IndexOf(form);
}
public bool IsSynchronized
{
get{return _forms.IsSynchronized;}
}
public object SyncRoot
{
get{return _forms.SyncRoot;}
}
public System.Collections.ICollection Keys
{
get{return _keys;}
}
public System.Collections.ICollection Values
{
get{return _forms;}
}
#endregion
}
}
The Form to Inheriet from
Code:
namespace System.Windows.Forms
{
/// <summary>
/// Extention of Form...
/// Represents a window or dialog box that makes up an application's user interface.
/// </summary>
/// <remarks>
/// -------------------------------------------------------------------------------
/// Method Extentions
/// -------------------------------------------------------------------------------
///
///
/// -------------------------------------------------------------------------------
/// Property Extentions
/// -------------------------------------------------------------------------------
/// Forms: typeof(FormsExCollection)
/// The static/shared Forms property acts as a replacement for the Forms collection
/// in versions of Visual Basic prior to .Net. FormEx objects
/// are added during thier construction and removed when they are disposed or
/// finalized. Note: FormEx object are not added to this
/// collection by default. They are added when a key is passed to thier contructor.
/// Thus only forms desired to be global are global and resources are conserved.
/// So if a System.Windows.FormEx is consructed with an in passed key it exists
/// in the FormEx.Forms collection until the form has been
/// disposed.
///
/// </remarks>
public class FormEx : System.Windows.Forms.Form
{
public FormEx(){}
public FormEx(string key)
{
if(key == null || key == "")
{
throw new System.SystemException("Invalid key.");
}
else
{
_forms.Add(this, key);
}
}
new public void Dispose()
{
if(_forms.Contains(this)){_forms.Remove(this);}
base.Dispose();
base.Dispose(true);
GC.SuppressFinalize(this);
}
private static FormExCollection _forms = new FormExCollection();
public static FormExCollection Forms
{
get{return _forms;}
set{_forms = value;}
}
}
}
Magiaus
If I helped give me some points.
-
Feb 5th, 2004, 04:37 PM
#2
PowerPoster
http://msdn.microsoft.com/library/de...adingtonet.asp
Whoops, in C# forum. Oh well, they have code that can be converted to C# just the same.
-
Feb 5th, 2004, 04:58 PM
#3
Thread Starter
Frenzied Member
I think most everyone understands VB anyway
Magiaus
If I helped give me some points.
-
Feb 7th, 2004, 03:17 PM
#4
Thread Starter
Frenzied Member
Stripped down VB.Net Version
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
This vb version dosn't support as much of the collection behavior for the FormsExCollection as the C# version but it can easly be added. I'm going to post this in the VB forum too I just thought you guys might like to see it.
Magiaus
If I helped give me some points.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|