|
|
.net里动态生成控件数组 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
从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); } } } }

|
|
相关文章:相关软件: |
|