这是 Form1 中的代码:
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
PbsWheel pbsw = new PbsWheel(pbs, pb, e.Delta, pb.AnimateRate, label2);
}
在form1的底部我有这个类:
public class AnimatedPictureBox : PictureBox
{
List<string> imageFilenames;
Timer t = new Timer();
public AnimatedPictureBox()
{
AnimateRate = 100; //It's up to you, the smaller, the faster.
t.Tick += Tick_Animate;
}
public int AnimateRate
{
get { return t.Interval; }
set { t.Interval = value; }
}
public void Animate(List<string> imageFilenames)
{
this.imageFilenames = imageFilenames;
t.Start();
}
public void StopAnimate()
{
t.Stop();
i = 0;
}
int i;
private void Tick_Animate(object sender, EventArgs e)
{
if (imageFilenames == null) return;
Load(imageFilenames[i]);
i = (i + 1) % imageFilenames.Count;
}
}
现在在新类中我有相同的类 AnimatedPcitureBox 和这段代码:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WeatherMaps
{
class PbsWheel
{
public PbsWheel(AnimatedPictureBox[] pbs, AnimatedPictureBox pb, int delta, int speed,Label label2)
{
//if (leave == true)
//{
for (int i = 0; i < pbs.Length; i++)
{
if (delta > 0)
{
if (speed < 5000)
{
if (speed < 1000)
{
speed += 100;
label2.Text = (speed / (double)1000).ToString();
}
else
{
speed += 1000;
label2.Text = (speed / 1000).ToString();
}
}
}
else
{
if (speed > 1000)
{
speed -= 1000;
label2.Text = (speed / 1000).ToString();
}
else
if (speed <= 1000 && speed > 100)
{
speed -= 100;
label2.Text = (speed / (double)1000).ToString();
}
}
}
//}
}
public class AnimatedPictureBox : PictureBox
{
List<string> imageFilenames;
Timer t = new Timer();
public AnimatedPictureBox()
{
AnimateRate = 100; //It's up to you, the smaller, the faster.
t.Tick += Tick_Animate;
}
public int AnimateRate
{
get { return t.Interval; }
set { t.Interval = value; }
}
public void Animate(List<string> imageFilenames)
{
this.imageFilenames = imageFilenames;
t.Start();
}
public void StopAnimate()
{
t.Stop();
i = 0;
}
int i;
private void Tick_Animate(object sender, EventArgs e)
{
if (imageFilenames == null) return;
Load(imageFilenames[i]);
i = (i + 1) % imageFilenames.Count;
}
}
}
}
但是当我在 Form1 中做这一行时:
PbsWheel pbsw = new PbsWheel(pbs, pb, e.Delta, pb.AnimateRate, label2);
我在 pbs 和 pb 上遇到 3 个错误。而 pbs 和 pb 都是 AnimatedPictureBox 类型。
错误 11 参数 1:无法从 'WeatherMaps.Form1.AnimatedPictureBox[]' 转换为 'WeatherMaps.PbsWheel.AnimatedPictureBox[]'
错误 12 参数 2:无法从“WeatherMaps.Form1.AnimatedPictureBox”转换为“WeatherMaps.PbsWheel.AnimatedPictureBox”
错误 10 'WeatherMaps.PbsWheel.PbsWheel(WeatherMaps.PbsWheel.AnimatedPictureBox[], WeatherMaps.PbsWheel.AnimatedPictureBox, int, int, System.Windows.Forms.Label)' 的最佳重载方法匹配有一些无效参数