.NET中添加控件数组
作者:cuike519的專欄?? http://blog.csdn.net/cuike519/
添加控件數組
在.NET里面我好像沒有找到有關于控件數組的說明,但是前兩天偶在網上看到了一篇關于如何在.NET里面實現控件數組的文章(該文章請參看MSDN).記得大學的時候在使用VB的時候使用過控件數組,可是到了.NET的時代好像沒有了.當時可以用控件數組作很多繁瑣的事情,可以動態的生成一些功能和目的基本相同的一組文本框和一堆標簽.這些控件數組響應同一個事件,我們在使用它的時候可以直接通過索引來訪問.我使用.NET以后好像綁定讓一切變的如此簡單,好像控件數組沒有什么用了,但是在前面的一次開發中我還是偶然的想到了使用控件數組,苦于不知道如何申明后來發現.NET不支持這個東東了.其實如果我們很了解FCL的話我想這個實現起來也不難!好了廢話就不說了下面開始我們的創建工作.這次我們創建的是Button的數組(也是MSDN上的例子.本文的原型以及代碼均來自MSDN,如果想要了解更多的詳細信息請參考MSDN的幫助),首先我們用到了CollectionBase (請參考http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionscollectionbaseclasstopic.asp) 類,該類提供了一個抽象的強類型集合的基類.我們可以用它來實現我們的控件數組.首先我們需要建立一個從CollectionBase繼承的類,我們稱之為ButtonArray.用來做存放Button的容器.下面是實現的代碼:
namespace ButtonArrayProject{
??????
?????? using System;
?????? /// <summary>
?????? /// ButtonArray 的摘要說明。
?????? /// </summary>
?????? public class ButtonArray : System.Collections.CollectionBase{
?
????????????? // 容器的一個引用,引用該控件數組所在的窗體的一個引用
????????????? private readonly System.Windows.Forms.Form HostForm;
?
????????????? // 添加一個新的按鈕
????????????? public System.Windows.Forms.Button AddNewButton(){
???????????????????? // 創建一個新的按鈕實例
???????????????????? System.Windows.Forms.Button aButton = new System.Windows.Forms.Button();
???????????????????? // 將該新的控件添加到(this)列表中
???????????????????? this.List.Add(aButton);
???????????????????? // 將控件添加到父窗體的控件集合中
???????????????????? HostForm.Controls.Add(aButton);
???????????????????? // 設置該控件的屬性
???????????????????? aButton.Top = Count * 25;
???????????????????? aButton.Left = 100;
???????????????????? // 用Button的Tag表示控件數組中的索引
???????????????????? aButton.Tag = this.Count;
???????????????????? aButton.Text = "Button " + this.Count.ToString();
???????????????????? // 添加一個事件響應
???????????????????? aButton.Click += new System.EventHandler(ClickHandler);
???????????????????? // 返回該新的控件
???????????????????? return aButton;
????????????? }
?
????????????? // 刪除控件數組中的最后一個元素
????????????? public void Remove(){
???????????????????? // 檢測該控件數組中是否有控件
???????????????????? if(this.Count > 0){
??????????????????????????? // 如果有則先從父容器中刪除最后一個元素
??????????????????????????? // 注意:此處是通過索引訪問
??????????????????????????? this.HostForm.Controls.Remove(this[this.Count-1]);
??????????????????????????? // 刪除控件數組中的最后一個控件
??????????????????????????? this.List.RemoveAt(this.Count-1);
???????????????????? }
????????????? }
?
????????????? // 添加一個事件出來函數
????????????? public void ClickHandler(Object sender, System.EventArgs e) {
???????????????????? // 用MessageBox返回一條消息
???????????????????? System.Windows.Forms.MessageBox.Show("你點擊的是 " +
??????????????????????????? ((System.Windows.Forms.Button) sender).Tag.ToString());
????????????? }
?
????????????? // 帶父窗體引用的構造函數
????????????? public ButtonArray(System.Windows.Forms.Form host){
???????????????????? // 為父窗體的引用賦值
???????????????????? this.HostForm = host;
???????????????????? // 添加一個默認的按鈕
???????????????????? this.AddNewButton();
????????????? }
?
????????????? // 控件數組的索引可以利用索引訪問(只能得到)
????????????? public System.Windows.Forms.Button this[int index]{
???????????????????? get{
??????????????????????????? return (System.Windows.Forms.Button)this.List[index];
???????????????????? }
????????????? }
?????? }
}
?
為了測試程序我們添加一個WinForm的工程.在該工程的Form1中放入兩個Button用來添加或者刪除控件數組中的控件.同時我們還需要在Form1中聲明一個ButtonArray的對象.在Form1的構造函數中實例該對象, 代碼如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ButtonArrayProject
{
?????? public class Form1 : System.Windows.Forms.Form{
????????????? private ButtonArray btnArray ;
????????????? private System.Windows.Forms.Button btnAdd;
????????????? private System.Windows.Forms.Button btnRemove;
????????????? private System.ComponentModel.Container components = null;
????????????? public Form1(){
???????????????????? InitializeComponent();
???????????????????? this.btnArray = new ButtonArray(this);
????????????? }
????????????? protected override void Dispose( bool disposing ){
???????????????????? if( disposing )
???????????????????? {
??????????????????????????? if (components != null)
??????????????????????????? {
?????????????????????????????????? components.Dispose();
??????????????????????????? }
???????????????????? }
???????????????????? base.Dispose( disposing );
????????????? }
????????????? #region Windows
????????????? private void InitializeComponent()???? {
???????????????????? this.btnAdd = new System.Windows.Forms.Button();
???????????????????? this.btnRemove = new System.Windows.Forms.Button();
???????????????????? this.SuspendLayout();
???????????????????? //
???????????????????? // btnAdd
???????????????????? //
???????????????????? this.btnAdd.Location = new System.Drawing.Point(352, 136);
???????????????????? this.btnAdd.Name = "btnAdd";
???????????????????? this.btnAdd.Size = new System.Drawing.Size(112, 23);
???????????????????? this.btnAdd.TabIndex = 0;
???????????????????? this.btnAdd.Text = "AddNewButton";
???????????????????? this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
???????????????????? //
???????????????????? // btnRemove
???????????????????? //
???????????????????? this.btnRemove.Location = new System.Drawing.Point(352, 192);
???????????????????? this.btnRemove.Name = "btnRemove";
???????????????????? this.btnRemove.Size = new System.Drawing.Size(112, 23);
???????????????????? this.btnRemove.TabIndex = 1;
???????????????????? this.btnRemove.Text = "RemoveButton";
???????????????????? this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
???????????????????? //
???????????????????? // Form1
???????????????????? //
???????????????????? this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
???????????????????? this.ClientSize = new System.Drawing.Size(512, 381);
???????????????????? this.Controls.Add(this.btnRemove);
???????????????????? this.Controls.Add(this.btnAdd);
???????????????????? this.Name = "Form1";
???????????????????? this.Text = "Form1";
???????????????????? this.ResumeLayout(false);
????????????? }
????????????? #endregion
????????????? [STAThread]
????????????? static void Main() {
???????????????????? Application.Run(new Form1());
????????????? }
????????????? private void btnAdd_Click(object sender, System.EventArgs e) {
???????????????????? this.btnArray.AddNewButton();
// 這里就是通過索引訪問的實例
???????????????????? this.btnArray[0].BackColor = Color.Blue;
????????????? }
????????????? private void btnRemove_Click(object sender, System.EventArgs e) {
???????????????????? this.btnArray.Remove();
????????????? }
?????? }
}
總結
以上是生活随笔為你收集整理的.NET中添加控件数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式之C#实现---- ProtoT
- 下一篇: 全国IP地址分配表