你的位置:首页 > ASP.net教程

[ASP.net教程]图片切换(timer控件与ImagesList结合使用)


  1. private void Form3_Load(object sender, EventArgs e)
  2.         {
  3.             //打开窗体的时候显示第一张图片
  4.             this.pictureBox1.Image = this.imageList1.Images[0];
  5.             //启动timer控件
  6.             this.timer1.Start();
  7.         }
  8. //计数器
  9. private int currIndex = 0;
  10. private bool flag = true;//开关
  11. private void timer1_Tick(object sender, EventArgs e)
  12.         {
  13.             if (flag)
  14.             {
  15.                 //判断是否为第一张
  16.        //不是第一张
  17.                 if (currIndex > 0)
  18.                     //显示上一张图片
  19.                     currIndex--;
  20.                 else
  21.                     //是第一张
  22.                     flag = false;
  23.             }
  24.             else
  25.             {
  26.                 //判断是否为第一张
  27.        //不是最后一张
  28.                 if (currIndex < this.imageList1.Images.Count - 1)
  29.                     //显示下下一张图片
  30.                     currIndex++;
  31.                 else//是最后一张
  32.                     flag = true;
  33.             }
  34.             this.pictureBox1.Image = this.imageList1.Images[currIndex];
  35.         }
  36. private bool flag1 = true;
  37. private void pictureBox1_Click(object sender, EventArgs e)
  38.         {
  39.             if (flag1)
  40.             {
  41.        //停止
  42.                 timer1.Stop();
  43.                 flag1 = false;
  44.             }
  45.             else
  46.             {
  47.                 //启动
  48.                 timer1.Start();
  49.                 flag1 = true;
  50.             }
  51.         }