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

207 阅读1分钟

一般在Form1.Designer.cs 文件下的 InitializeComponent 函数中进行

  this .SuspendLayout();//公有,在窗体创建时自动创建

\

\

Timer控件

1  申明

private System.Windows.Forms. Timer Timer_j;

\

2  实例化

this .components = new System.ComponentModel. Container ();

this .Timer_j = new System.Windows.Forms. Timer ( this .components);

\

3  设置

//

// Timer_j

//

this .Timer_j.Interval = 5000;//定时器初始化

this .Timer_j.Enabled = true ;//定时器使能

this .Timer_j.Tick += new System. EventHandler ( this .Timer_j_Tick);//添加触发时间

\

\

\

Button控件

1  申明

private System.Windows.Forms. Button button_start;

private System.Windows.Forms. Button button_stop;

\

2  实例化

this .button_start = new System.Windows.Forms. Button ();

this .button_stop = new System.Windows.Forms. Button ();

\

3  设置

//

// button_start

//

this .button_start.Location = new System.Drawing. Point (200, 200);

this .button_start.Name = "button_start" ;

this .button_start.Size = new System.Drawing. Size (50, 20);

this .button_start.Text = "开始" ;

this .button_start.TabIndex = 1;

//

// button_stop

//

this .button_stop.Location = new System.Drawing. Point (400, 200);

this .button_stop.Name = "button_stop" ;

this .button_stop.Size = new System.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  申明

private System.Windows.Forms. Label label_dis;

\

2  实例化

this .label_dis = new System.Windows.Forms. Label ();

\

3  设置

//

// label_dis

//

this .label_dis.Location = new System.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);

\