
|
Visual C# Windows Form Custom ArrayList Example
|
|---|
|
Visual C# Windows Form Custom ArrayList Example
Learn how to create type-safe collections in Visual C# with by inhereting from the System.Collections.ArrayList class.
Category: .Net Framework, Visual C#, Windows Forms, Collections,
Posted 12/13/2002,
By Mike G.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CustomArrayListExample
{
///
/// Summary description for Form1.
///
public class frmCustomArrayList : System.Windows.Forms.Form
{
///
/// Creates a new instance of the Custom ArrayList.
///
private DataSetArrayList dsArrayList = new DataSetArrayList();
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnRemove;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblCurrent;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public frmCustomArrayList()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnRemove = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.lblCurrent = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblCurrent,
this.label1,
this.btnRemove,
this.btnAdd});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(416, 160);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Custom ArrayList Example by MTG - dotnetfun.com";
//
// btnAdd
//
this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnAdd.Location = new System.Drawing.Point(8, 64);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(400, 40);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add a DataSet Objects to DataSetArrayList";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnRemove.Location = new System.Drawing.Point(8, 112);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(400, 40);
this.btnRemove.TabIndex = 1;
this.btnRemove.Text = "Remove a DataSet Objects to DataSetArrayList";
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(248, 16);
this.label1.TabIndex = 2;
this.label1.Text = "Current DataSet objects in the Custom ArrayList:";
//
// lblCurrent
//
this.lblCurrent.Location = new System.Drawing.Point(248, 32);
this.lblCurrent.Name = "lblCurrent";
this.lblCurrent.Size = new System.Drawing.Size(100, 16);
this.lblCurrent.TabIndex = 3;
//
// frmCustomArrayList
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 174);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
this.Name = "frmCustomArrayList";
this.Text = "MTG\'s Custom System.Collections.ArrayList UDT";
this.Load += new System.EventHandler(this.frmCustomArrayList_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new frmCustomArrayList());
}
///
/// Adds a new, empty DataSet object to the custom collection.
///
///
///
private void btnAdd_Click(object sender, System.EventArgs e)
{
dsArrayList.Add(new DataSet());
UpdateCollectionCount();
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
if (dsArrayList.Count > 0)
dsArrayList.Remove(dsArrayList[dsArrayList.Count - 1]);
else
MessageBox.Show("There are no more DataSet objects in the " +
"collection to remove!");
UpdateCollectionCount();
}
private void UpdateCollectionCount()
{
lblCurrent.Text = dsArrayList.Count.ToString();
}
private void frmCustomArrayList_Load(object sender, System.EventArgs e)
{
UpdateCollectionCount();
}
///
/// This creates a new class inherited from the base
/// System.Collections.ArrayList class so that only
/// Objects of type DataSet can be added and removed
/// from the collection.
///
private class DataSetArrayList : System.Collections.ArrayList
{
public void Add(DataSet ds)
{
base.Add(ds);
}
public void Remove(DataSet ds)
{
base.Remove(ds);
}
public System.Data.DataSet this [int Index]
{
get
{
return (System.Data.DataSet) base[Index];
}
set
{
base[Index] = value;
}
}
}
}
}
|
|