码迷,mamicode.com
首页 > Windows程序 > 详细

关联事件,向窗体中添加控件,设置控件属性等系列操作

时间:2014-12-28 22:01:26      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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         }
View Code
技术分享
 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         }
View Code

添加事件后 要在项目中写出事件代码

可以自己新建一个From窗体在其中使用图形化面板添加 然后在

public Form1()
{
InitializeComponent();
}

中查看代码

关联事件,向窗体中添加控件,设置控件属性等系列操作

标签:

原文地址:http://www.cnblogs.com/xzh1993/p/4190423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!