定义了一个计时器控件,两个按钮,一个listview
点击启动按钮时,每秒在listview中显示一项,点击停止按钮时停止计时器功能。可是当我再次点击开始按钮时,每秒钟在listview显示两项,不知道为什么,如果在停止后,开始,每秒钟就会显示三项,依次类推,private static int a = 1;
        ListViewItem lvItem = null;
        ListViewItem.ListViewSubItem lvsItem = null;   private void timer1_Tick(object sender, EventArgs e)
        {
            a++;
            lvItem = new ListViewItem(a.ToString());            lvsItem = new ListViewItem.ListViewSubItem();
            lvsItem.Text = a.ToString();
            lvItem.SubItems.Add(lvsItem);            this.listView1.Items.Add(lvItem);        }        private void button1_Click(object sender, EventArgs e)
        {
            this.timer1.Tick += new EventHandler(timer1_Tick);            this.timer1.Interval = 1000;
            this.timer1.Start();
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
        }

解决方案 »

  1.   

    this.timer1.Tick   +=   new   EventHandler(timer1_Tick); 
    这一句使你的程序每次点击button1之后都要多执行一次timer1_Tick()方法
      

  2.   

    private   void   button1_Click(object   sender,   EventArgs   e) 
                    { 
                            this.timer1.Tick   +=   new   EventHandler(timer1_Tick);                         this.timer1.Interval   =   1000; 
                            this.timer1.Start(); 
                    } 
    太麻烦了吧
    private   void   button1_Click(object   sender,   EventArgs   e) 
                    { 
                           this.timer1.Interval=1000;
                           this.timer1.Enable=true;
                    } private   void   button2_Click(object   sender,   EventArgs   e) 
                    { 
                            this.timer1.Enable=false;
                    }
    就可以了
    不需要每次都重叠添加事件.
      

  3.   

    不需要在按钮事件下添加事件,在Load事件或者在Initial里面this.timer1.Tick  +=   new  EventHandler(timer1_Tick);   
      

  4.   

    this.timer1.Tick += new EventHandler(timer1_Tick);
    不要放在button1_Click内。+=会使timer1_Tick“delegate”多个Tick事件,使timer1_Tick同时多次触发。@_@ 我刚回答了这个帖子。
    http://topic.csdn.net/u/20080113/21/719ddab2-6539-48b5-9f43-56bbcae8a19a.html
      

  5.   

    InitializeComponent()方法里
    this.timer1.Interval=1000; 
    this.timer1.Enable=false; 
    this.timer1.Tick+=new EventHandler(timer1_Tick);  private       void       button1_Click(object       sender,       EventArgs       e)   
                                    {   
                                                  this.timer1.Enable=true; 
                                    }   private       void       button2_Click(object       sender,       EventArgs       e)   
                                    {   
                                                    this.timer1.Enable=false; 
                                    } 
      

  6.   

    this.timer1.Tick   +=   new   EventHandler(timer1_Tick); 
    会触发 timer1_Tick(object   sender,   EventArgs   e) 
    当然会多了,楼上均有解,不多语!
      

  7.   

    每点击一次按钮你就new一个时间控件,但然就会多显示一个。
    如下修改代码即可private       void       button1_Click(object       sender,       EventArgs       e)   
                                    {   
                                                  this.timer1.Interval=1000; 
                                                  this.timer1.Enable=true; 
                                    }   private       void       button2_Click(object       sender,       EventArgs       e)   
                                    {   
                                                    this.timer1.Enable=false; 
                                    }