C# 下利用代码创建按钮、定时器和标签

来源:互联网 发布:珍珠控台软件 编辑:程序博客网 时间:2024/05/29 08:53
一般在Form1.Designer.cs 文件下的InitializeComponent函数中进行
 this.SuspendLayout();//公有,在窗体创建时自动创建


Timer控件
1  申明
privateSystem.Windows.Forms.TimerTimer_j;

2  实例化
this.components =newSystem.ComponentModel.Container();
this.Timer_j =newSystem.Windows.Forms.Timer(this.components);

3  设置
//
// Timer_j
//
this.Timer_j.Interval = 5000;//定时器初始化
this.Timer_j.Enabled =true;//定时器使能
this.Timer_j.Tick +=newSystem.EventHandler(this.Timer_j_Tick);//添加触发时间



Button控件
1  申明
privateSystem.Windows.Forms.Buttonbutton_start;
privateSystem.Windows.Forms.Buttonbutton_stop;

2  实例化
this.button_start =newSystem.Windows.Forms.Button();
this.button_stop =newSystem.Windows.Forms.Button();

3  设置
//
// button_start
//
this.button_start.Location =newSystem.Drawing.Point(200, 200);
this.button_start.Name ="button_start";
this.button_start.Size =newSystem.Drawing.Size(50, 20);
this.button_start.Text ="开始";
this.button_start.TabIndex = 1;
//
// button_stop
//
this.button_stop.Location =newSystem.Drawing.Point(400, 200);
this.button_stop.Name ="button_stop";
this.button_stop.Size =newSystem.Drawing.Size(50, 20);
this.button_stop.Text ="结束";
this.button_stop.TabIndex = 2;

4  加入窗口
this.Controls.Add(this.button_start);
this.Controls.Add(this.button_stop);


Label控件
1  申明
privateSystem.Windows.Forms.Labellabel_dis;

2  实例化
this.label_dis =newSystem.Windows.Forms.Label();

3  设置
//
// label_dis
//
this.label_dis.Location =newSystem.Drawing.Point(200, 100);
this.label_dis.Name ="label_dis";
this.label_dis.AutoSize =true;//自动调节大小
this.label_dis.Text ="ljj";
this.label_dis.TabIndex = 4;

4  加入窗口
this.Controls.Add(this.label_dis);

0 0
原创粉丝点击