Results 1 to 4 of 4

Thread: VB6 Forms Collection

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Red face 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.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  3. #3

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    I think most everyone understands VB anyway
    Magiaus

    If I helped give me some points.

  4. #4

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Stripped down VB.Net Version

    VB Code:
    1. Public Class FormEx
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.     Public Sub New()
    5.         MyBase.New()
    6.     End Sub
    7.  
    8.     Public Sub New(ByVal key As String)
    9.         MyBase.New()
    10.         _formsex.Add(Me, key)
    11.     End Sub
    12.  
    13.     Private Shared _formsex As FormsExCollection = New FormsExCollection()
    14.     Public Shared Property Forms() As FormsExCollection
    15.         Get
    16.             Return _formsex
    17.         End Get
    18.         Set(ByVal Value As FormsExCollection)
    19.             _formsex = Value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Overloads Overrides Sub Dispose()
    24.         If _formsex.Contains(Me) Then _formsex.Remove(Me)
    25.         MyBase.Dispose(True)
    26.         GC.SuppressFinalize(True)
    27.     End Sub
    28. End Class
    29.  
    30. Public Class FormsExCollection
    31.     Private _keys As System.Collections.ArrayList = New System.Collections.ArrayList()
    32.     Private _formsex As System.Collections.ArrayList = New System.Collections.ArrayList()
    33.  
    34.     Friend Function Add(ByRef formex As FormEx, ByVal key As String) As Integer
    35.         _keys.Add(key)
    36.         Return _formsex.Add(formex)
    37.     End Function
    38.  
    39.     Friend Sub Remove(ByRef formex As FormEx)
    40.         _keys.RemoveAt(_formsex.IndexOf(formex))
    41.         _formsex.Remove(formex)
    42.     End Sub
    43.  
    44.     Public ReadOnly Property Count() As Integer
    45.         Get
    46.             Return _formsex.Count()
    47.         End Get
    48.     End Property
    49.  
    50.     Public Function Contains(ByRef formex As FormEx) As Boolean
    51.         Return _formsex.Contains(formex)
    52.     End Function
    53.     Public Property Item(ByVal index As Integer) As FormEx
    54.         Get
    55.             Return CType(_formsex(index), FormEx)
    56.         End Get
    57.         Set(ByVal Value As FormEx)
    58.             _formsex(index) = Value
    59.         End Set
    60.     End Property
    61.     Public Property Item(ByVal key As String) As FormEx
    62.         Get
    63.             Return CType(_formsex(_keys.IndexOf(key)), FormEx)
    64.         End Get
    65.         Set(ByVal Value As FormEx)
    66.             _formsex(_keys.IndexOf(key)) = Value
    67.         End Set
    68.     End Property
    69. 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
  •  



Click Here to Expand Forum to Full Width