日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

.NET中添加控件数组

發布時間:2023/11/27 生活经验 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .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;

???????????????????? // ButtonTag表示控件數組中的索引

???????????????????? 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中添加控件数组的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。