我创建了一个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)
{ }
}
}
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)
{ }
}
}
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);这样试下。
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)
{ }
}
}
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;
}
这个code在,t.Tick == null 上有问题
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; }
}