为了在写程序时更加方便,程序员通常会创建自己常用的控件。那么 C# 动态控件是怎样创建的呢?今天我们就来学习 C# 动态创建 控件

htmltable
tablerow
tablecell
 动态创建Form
直接从标准控件中继承过来
 动态创建Table
htmltable ht1=new htmltable();
 动态创建tablerow,tablecell
tablerow tr = new tablerow
tablecell tc = new tablecell
在form中添加控件
form1.controls.add(table);
在htmltable中添加行htmltablerow
htmltable.rows.add(htmltablerow);
在htmltablerow中添加htmltablecell
htmltablerow.cells.add(htmltablecell);
在htmltable中添加其他控件
htmltable.controls.add(OtherControl)
 设置属性
label.text="sadfsf"直接设置。。
 设置背景颜色
htmltable.attributes["background"]="a.jpg";
private void InitializeComponent()
{
Form1 = new HtmlForm();
this.Controls.Add(Form1);
Label lb1 = new Label();
Label lb2 = new Label();
tb1 = new TextBox();
TextBox tb2 = new TextBox();
Label lb3 = new Label();
Label lb4 = new Label();
ddl4 = new DropDownList();
TableRow tr =new TableRow();
DropDownList ddl1 = new DropDownList();
Form1.Controls.Add(lb1);
lb1.Text = "会员卡号";
Form1.Controls.Add(tb1);
lb2.Text = "姓名";
Table table1 = new Table();
Form1.Controls.Add(table1);
table1.Rows.Add(tr);
TableCell tabcl1 = new TableCell();
tr.Cells.Add(tabcl1);
tabcl1.Controls.Add(lb2);
Form1.Controls.Add(tb2);
table1.Rows.Add(tr);
tr.Cells.Add(tabcl1);
tabcl1.Controls.Add(ddl1);
HtmlTable ht = new HtmlTable();
Form1.Controls.Add(ht);
ht.Border = 0;
ht.CellPadding = 0;
ht.CellSpacing = 0;
ht.Height = "100";
ht.Width = "100%";E
ht.Attributes["background"]="a.jpg";
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell htc = new HtmlTableCell();
ht.Rows.Add(htr);
htr.BorderColor="red";
htr.Height = "70";
htr.Cells.Add(htc);
htc.BorderColor="black";
htc.Height="50";
htc.Controls.Add(lb4);
lb4.Text = "asdfsf";
Form1.Controls.Add(ddl4);
ddl4.Items.Add(new ListItem("男","M"));
ddl4.Items.Add(new ListItem("女","F"));
}