我创建了一个pictureBox1并让它移动,并设定移动速度不变,可是每次连续点击StartButton时移动速度会成倍增加,我想保持恒定的速度,该怎么办啊?下面是我的code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
       
       
        Timer t = new Timer();
        
        public Form1()
        {
            InitializeComponent();
           
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
        }        private void t_Tick(object sender, EventArgs e)
        {
            this.pictureBox1.Left +=1;
            if (this.pictureBox3.Left >= 800)
            t.Dispose();
            
        }        private void StartButton_Click(object sender, EventArgs e)
        {
              
                this.pictureBox1.Left = 170;
                
                t.Interval = 30;
                t.Tick += new EventHandler(t_Tick);
                t.Enabled = true;
                
        }        private void StopButton_Click(object sender, EventArgs e)
        {             
            t.Dispose();
        }        private void pictureBox1_Click(object sender, EventArgs e)
        {        }
         
    }
     
}

解决方案 »

  1.   

    应该跟用什么板没关系,第一次点击StartButton后,picutreBox1的横坐标以一个单位的速度增加,当我再点击StartButton时,picutreBox1的横坐标就以两个单位的速度增加,如此成倍增加,移动的速度就越来越快!这是问题的所在。
      

  2.   

    主要是你不了解 time的原应,t.Tick每次会单独启动一个线程,你这样写是对同一个picturebox进行操作,也就是说连续点击的话就会有N个线程在调用picturebox,当然会加速,一种方法是加一个逻辑变量,当start时把它fasle了,当stop是在true,不能让它连续点击,第二种是保持一直只有一个t.Tick事件调用picturebox,也就是说在能t.Tick+=,也就能t.Tick-=了,把前一个t.Tick给干掉,就好了,试试吧。我觉得是这样
      

  3.   


            private void StartButton_Click(object sender, EventArgs e)
            {
                 
                    this.pictureBox1.Left = 170;
                   
                    t.Interval = 30;
                    t.Tick += new EventHandler(t_Tick);
                    t.Enabled = true;
                   
            } 这段代码的问题。标记颜色处,假设你第二次点击按钮
    t.Tick = EventHandler(t_Tick) + EventHandler(t_Tick);
    注册了两个事件,(PS:这个按钮你按一次会快一次)
    试着修改下代码吧。。t.Tick = new EventHandler(t_Tick);这样试下。
      

  4.   

                this.pictureBox1.Left +=1; 
                if (this.pictureBox3.Left >= 800) //这里的picturebox3应该是pictureBox1吧
                t.Dispose();         private void StartButton_Click(object sender, EventArgs e) 
            { 
                  
                    this.pictureBox1.Left = 170; 
                    
                    t.Interval = 30; 
                    t.Tick += new EventHandler(t_Tick); 
                    t.Enabled = true; 
                    
            } 
    明白以上的                t.Tick += new EventHandler(t_Tick); 
    中的+=什么意思吗?这里的事件是串接的,如果你多次调用这个语句(就是你button点击多次),那么嘀嗒到来的时候,将运行t_Tick你按了button的次数,也就是你按了3次,一次嘀嗒就运行t_Tick3次,那么this.pictureBox1.Left +=1;就变成了运行3次,就是this.pictureBox1.Left加了3,所以你按的次数越多,速度越快。
    如果修改的话,建议如下
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; namespace WindowsFormsApplication2 

        public partial class Form1 : Form 
        { 
          
          
            Timer t = new Timer(); 
            
            public Form1() 
            { 
                InitializeComponent(); 
              
            } 
            private void Form1_Load(object sender, EventArgs e) 
            { 
                t.Tick += new EventHandler(t_Tick); 
            }         private void t_Tick(object sender, EventArgs e) 
            { 
                this.pictureBox1.Left +=1; 
                if (this.pictureBox3.Left >= 800) 
                    t.Enabled = false;
                
            }         private void StartButton_Click(object sender, EventArgs e) 
            { 
                  
                    this.pictureBox1.Left = 170; 
                    
                    t.Interval = 30; 
                    t.Enabled = true; 
                    
            }         private void StopButton_Click(object sender, EventArgs e) 
            {            
                t.Enabled = false;
            }         private void pictureBox1_Click(object sender, EventArgs e) 
            {         } 
            
        } 
        
    }
      

  5.   

    6楼正解
    private void t_Tick(object sender, EventArgs e)
    {
      this.pictureBox1.Left +=1;
      if (this.pictureBox3.Left >= 800)
        t.Dispose();
    }private void StartButton_Click(object sender, EventArgs e)
    {
      this.pictureBox1.Left = 170;
                   
      t.Interval = 30;
      if (t.Tick == null)
      {
        t.Tick += new EventHandler(t_Tick);
      }
      t.Enabled = true;

      

  6.   


    这个code在,t.Tick == null 上有问题
      

  7.   

    我给的两个方案中加逻辑变量好实现,如果想把委托事件给移出,好像有问题,在移出时会创建新的函数指针,本想给个代码呢,算了,8楼的意思就是保持只有一个t.tick。
    private void button1_Click(object sender, EventArgs e)
            {
                timer1.Interval = 10;
                if (!timer1.Enabled)
                {
                    timer1.Tick += new EventHandler(timer1_Tick);
                    timer1.Enabled = true;
                }
                else
                {
                    //timer1.Tick -= new EventHandler(timer1_Tick);
                    //timer1.Tick += new EventHandler(timer1_Tick);
                    timer1.Enabled = false;
                }
            }        void timer1_Tick(object sender, EventArgs e)
            {
                if (this.textBox1.Left > this.Right)
                {
                    this.textBox1.Left = this.Left;
                }
                else { this.textBox1.Left += 1; }
            }