.net里动态生成控件数组

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

从BASIC开始接触开发的占到了绝大多数,而使用VB起家开始接触win开发也是占了绝 大多数,从vb到vb.net变化确实不小,以前用的很多的控件数组在.net里却没有了,但是费点时间就OK了,这个是我的一个程序里用到的,今天把这 个类贴上来,这个是很早以前从MSDN上看到的,根据他讲的来做的。

//*********************************

//动态增加,删除数组控件

//做课题设计的同学可以引用这个类

//范维肖

//*********************************

namespace gradeSystem

{

//**************************

//类textBoxArray实现了动态增加和删除

//textBox的功能

//**************************

public class textBoxArray:System.Collections.CollectionBase

{

private readonly System.Windows.Forms.Form HostForm;

//构造函数

public textBoxArray(System.Windows.Forms.Form Host)

{

HostForm=Host;

}

//addNewTextBox方法添加一个textbox控件

public void addNewTextBox()

{

if(this.Count<7)

{

//建一个新的textbox实例.

System.Windows.Forms.TextBox aTextBox=new System.Windows.Forms.TextBox();

//将其添加到集合的内部列表

this.List.Add(aTextBox);

//将TextBox添加到由HostForm字段引用的窗体的集合列表中

HostForm.Controls.Add(aTextBox);

//设置初始属性

aTextBox.Left=(Count-1)*130+70;

aTextBox.Top=160;

aTextBox.Width=120;

aTextBox.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;

aTextBox.Font=new System.Drawing.Font("Verdana", 40, System.Drawing.FontStyle.Bold);

aTextBox.Tag=this.Count;

//初始值

aTextBox.Text="9.9";

aTextBox.ForeColor=System.Drawing.Color.Red;

}

}

//创建索引

public System.Windows.Forms.TextBox this [int index]

{

get

{

return (System.Windows.Forms.TextBox)this.List[index];

}

}

//Remove方法用来删除控件

public void Remove()

{

if(this.Count>0)

{

HostForm.Controls.Remove (this[this.Count -1]);

this.List.RemoveAt(this.Count-1);

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航