重写控件,动态加载控件

一. 重写控件

个人理解重写控件的意义:需要多次添加,但是只要部分功能,直接写成方法后,方便设置,不用重复设置,提高代码重用性

例子:

class lab : Label//例子

{

public Label CreateLab(string labName, int x, int y, int w, int h, string text)

{

this.Name = labName;

this.AutoSize = true;

this.Location = new System.Drawing.Point(x, y);

this.Size = new System.Drawing.Size(w, h);

this.Text = text;

return this;

}

}

private void button1_Click(object sender, EventArgs e)//调用

{

lab l = new lab();

l.CreateLab("lab", 10, 29, 20, 20, "完成");

this.Controls.Add(l);//动态添加

}

再次用的时候就不用每个属性去设置了

posted @ 2008-04-21 10:33  shangb  阅读(457)  评论(0编辑  收藏  举报