.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中添加控件数组相关推荐
- 学习笔记:VB.net动态添加控件数组并传递事件
学习笔记:VB.net动态添加控件数组并传递事件 控件数组和事件 "中间人" 动态添加控件 控件数组和事件 新建一个用户窗体,在定义控件数组时,不能用Withevnets来定义数组 ...
- java控件数组_在C# WinForm程序中创建控件数组及相应的事件处理
控件数组是VB提供的一个优秀的设计解决方案,它能很方便快捷的处理大批同类控件的响应和时间处理,但不知为什么在C#中这个优秀特性没有传承下来,甚为可惜,本文将要探讨就是如何在C# WinForm程序实现 ...
- 在 Visual Basic .NET 和 Visual C# .NET 中创建控件数组
在 Visual Basic .NET 和 Visual C# .NET 中创建控件数组 摘要:本文介绍如何使用 Visual Basic® .NET 和 Visual C#™ .NET 创建和管理控 ...
- VS2010向工具箱中添加控件解决 Microsoft Communications Control,未能实例化 设计时授权
第一步是Visual Studio 2010中添加注册控件的方法 在VC6.0中添加ADO Data Control等控件是很很方便的,"Project" --> " ...
- (VC2005)MFC中添加控件的成员变量.
VC 2005 中添加控件的成员变量. 方法一:使用变量添加向导. 右击控件->Add Variable (打开了 Add Member Variable Wizard) -> 输入Va ...
- VS2008向工具箱中添加控件解决 未能实例化 设计时授权
VS2010同理 VS2008向工具箱中添加串口控件,步骤为工具--选择工具箱项,在出现的选择工具箱项窗体中选择COM组件勾选Microsoft Communications Control, ver ...
- Android 中动态的向布局中添加控件
先看一下效果图: 注: 这里使用的是一个自定义的布局文件,你可以向这个布局文件中添加任何控件,它也会动态的依据控件 的大小,动态的排列控件的分布 注:这里使用到的更新界面的方法:http://blog ...
- FastReport安装包下载、安装、去除使用限制以及工具箱中添加控件
场景 FastReport .NET 2019是一款适用于Windows Forms, ASP.NET和MVC框架的功能齐全的报表分析解决方案.可用在Microsoft Visual Studio 2 ...
- 计算机二级vb知识点汇总,计算机等级考试二级VB考点:控件数组
控件数组是由一组相同类型的控件组成的,它们共用一个控件名,具有相同的数组.控件数组适用于若干个控件执行的操作相似的场合,控件组共享同样的事件过程.下面是小编为大家带来的关于控件数组的知识,欢迎阅读. ...
最新文章
- Python+selenium 自动化-通过窗口名切换窗口,如何获取当前窗口的title窗口名
- 焊接产生的问题和解决方法
- Python 使用@property对属性进行数据规范性校验
- matlab loopcount,求助一个数值积分问题,用matlab的quadgk函数来计算,谢谢!
- visualSVN下载与安装
- Spring Cloud构建微服务架构:分布式服务跟踪(跟踪原理)【Dalston版】
- 最新!MongoDB 重磅发布 MongoDB 5.0 和无服务器 Atlas
- Hadoop入门基础教程 Hadoop之完全分布式环境搭建
- 计算机三级-数据库-数据库应用系统开发方法
- ruby和python比较_为什么说Ruby比Python容易阅读
- a73*2+a53*2指的是什么_篮球内外线是什么意思?篮球外线是什么位置-百科-
- ajax写入txt,javascript结合ajax读取txt文件内容
- 怎样去构建一个优质的Docker容器镜像
- 官方demo修改后的webuploader上传预览图片(兼容IE8) github下载回来的有问题
- VC++6.0 DDK 环境配置
- 计算机技术在中医药的应用,计算机技术在中医药领域的应用概况
- distpicker
- 图片去水印的原理_去水印简单操作:图图去水印
- 为什么平方损失函数不适应于分类问题?——从概率论的角度
- 季琦谈创业:三对矛盾和三个关口[转]
热门文章
- 根据镜像安装oracle插件,docker镜像alpine中安装oracle客户端
- PCL:点云数据基于法线的边界提取(从最初的法线估计理论推导到最终的边界提取)
- 【图像分类案例】(2) DenseNet 天气图片四分类(权重迁移学习),附Tensorflow完整代码
- 【深度学习理论】(2) 卷积神经网络
- 【Pandas库】(2) Series的基本操作方法
- CornerNet的配置、训练与测试
- OpenCV——canny算子
- layui 表格内容写temple函数_layui 表格内容显示更改
- 深入浅出的讲解傅里叶变换(完整)
- 只需三分钟您就可以用nodejs搭建静态网页服务器(配置静态网页访问目录)