1 private void Form1_Load(object sender, EventArgs e) 2 { 3 Label lb1=new Label(); 4 lb1.Text="123"; 5 Label lb2 = new Label(); 6 lb2.Text = "234"; 7 Label lb3 = new Label(); 8 lb3.Text = "345"; 9 TextBox tb = new TextBox(); 10 tb.Text = "textboxTest"; 11 tb.TextChanged+=new EventHandler(tb_TextChanged); 12 tb.Validating+=new CancelEventHandler(tb_Validating); 13 TableLayoutPanel tbl = new TableLayoutPanel(); 14 tbl.ColumnCount = 5; 15 tbl.RowCount = 5; 16 for (int i = 0; i < tbl.RowCount; i++) 17 { 18 tbl.RowStyles.Add(new ColumnStyle(SizeType.Percent, 20F)); 19 } 20 for (int i = 0; i < tbl.ColumnCount; i++) 21 { 22 tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F)); 23 } 24 tbl.Dock = DockStyle.Fill; 25 tbl.Controls.Add(lb1,0,0); 26 tbl.Controls.Add(lb2,1,0); 27 tbl.Controls.Add(lb3,0 ,2); 28 tbl.Controls.Add(tb, 0, 3); 29 this.Controls.Add(tbl); 30 }
1 private void tb_TextChanged(object sender, EventArgs e) 2 { 3 MessageBox.Show("改变"); 4 } 5 private void tb_Validating(object sender, CancelEventArgs e) 6 { 7 Control ctl = (Control)sender; 8 if (!ctl.Text.IsNumeric()) 9 { 10 MessageBox.Show("请输入数字"); 11 e.Cancel = true; 12 } 13 }
添加事件后 要在项目中写出事件代码
可以自己新建一个From窗体在其中使用图形化面板添加 然后在
public Form1()
{
InitializeComponent();
}
中查看代码