WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日

时间:2023-03-08 22:06:23

用户控件:

通过布局将多个控件整合为一个控件,根据自己的需要进行修改,可对用户控件内的所有控件及控件属性进行修改使用

动态创建添加控件:

                 //定义控件类型
                 Button btn = new Button();
                  //控件名称……等属性,也可以直接绑定各种事件
                 btn.Name = "mybutton" + i.ToString();
                 //添加到窗体   this 可以替换为  容器控件
                 this.Controls.Add(btn);

timer控件:

  代码创建绑定事件

     System.Timers.Timer t = );   //实例化Timer类,设置间隔时间为10000毫秒;
     t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;
     t.AutoReset = true;   //设置是执行一次(false)还是一直执行(true);
     t.Enabled = true;     //是否执行System.Timers.Timer.Elapsed事件;   

     public void theout(object source, System.Timers.ElapsedEventArgs e)
      {
         MessageBox.Show("OK!");
      }  

  设计界面

1.将timer拖到界面上,设定属性值,时间间隔等;

2.在事件中双击自动生成如theout的方法;

3. 调用方法,在要调用的按钮事件等内,timer.start(),timer.stop() 这样就行了。