C#學習Timer


      Timer類提供以指定的時間間隔執行方法的機制。此類不能繼承。Timer能有規律的以一定的時間間隔激發timer事件,而執行相應的程序代碼。Timer控件的Interval屬性表示兩個計時器事件之間的時間間隔,其值以ms為單位。Timer將每隔Interval觸發一次計時器事件Tick。如:

namespace mytimer
{
   public partial class Timer:Form
  {
      public Timer()
      {
       InitializeComponent();
       }
private int direction = 5;

private void frmTimer_Load(object sender,EventArgs e)
{
       cbInterval.SelectedIndex = 1;
}

private void btnMove_Click(object sender,EventArgs e)
{
       timer1.Enabled = true;
}

private void timer_Tick(object sender,EventArgs e)
{
       if(lblLogo.Left <= 0|| lblLogo.Right >= this.Width)
       {
            direction = -direction;
       }
       lblLogo.Left -= direction;
}

private void cbInterval SelectedIndexChanged(object sender,EventArgs e)
{
       timer1.Interval = int.Parse(cbInterval.Text);
} 
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM