C# 将一些代码移到新类中,为什么im无法转换AnimatedPictureBox[]pbs上的错误?
这是表1中的代码:C# 将一些代码移到新类中,为什么im无法转换AnimatedPictureBox[]pbs上的错误?,c#,winforms,C#,Winforms,这是表1中的代码: private void Form1_MouseWheel(object sender, MouseEventArgs e) { PbsWheel pbsw = new PbsWheel(pbs, pb, e.Delta, pb.AnimateRate, label2); } 在表格1的底部,我有一节课: public class AnimatedPictureBox : PictureBox { List<string> imageFilenam
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
PbsWheel pbsw = new PbsWheel(pbs, pb, e.Delta, pb.AnimateRate, label2);
}
在表格1的底部,我有一节课:
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;
}
}
我在pbs和pb上得到3个错误。pbs和pb是动画的PictureBox类型
错误11参数1:无法从“WeatherMaps.Form1.AnimatedPictureBox[]”转换为“WeatherMaps.PBSWeel.AnimatedPictureBox[]”
错误12参数2:无法从“WeatherMaps.Form1.AnimatedPictureBox”转换为“WeatherMaps.PBSWeel.AnimatedPictureBox”
错误10“WeatherMaps.pbsweel.pbsweel(WeatherMaps.pbsweel.AnimatedPictureBox[],WeatherMaps.pbsweel.AnimatedPictureBox,int,int,System.Windows.Forms.Label)”的最佳重载方法匹配具有一些无效参数
实际上,
AnimatedPictureBox
有两个单独的类,就像Form1
和WeatherMaps
中的内部类一样。代码和名称是否相同并不重要——类是不同的
你最好把它转移到一个新的班级,而不是其他班级。最好您也可以将其移动到自己的文件中。错误很明显 在不同的名称空间中有两个同名的类
创建一个新文件并将其放在那里,以两种形式使用它/PbsWeel另一种方法是创建一个自定义控件。通过将自定义控件添加到
Form1
,Form1.Designer
将为您完成任务 好的,把它移到一个新的类,它现在正在工作。但现在车轮什么也没做。新的PBSWEELL级车轮正在获得增量。在form1中的form1鼠标滚轮事件中,我做了:pbsweel pbsw=新的pbsweel(pbs,pb,e.Delta,pb.AnimateRate,label2);但由于某种原因,当我在程序运行时滚动/移动鼠标滚轮时,它什么也不做。当你调试时,它会调用所有传递增量的相关方法吗?是的,我看到e.delta被传递给了增量,速度也会再次检查。那么,你认为会发生什么呢?我更改了它,删除了速度变量,而不是更改了PBSWeel类中的所有速度变量,并将其替换为pbs[i]。动画制作,现在它可以工作了。谢谢
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;
}
}
}
}
PbsWheel pbsw = new PbsWheel(pbs, pb, e.Delta, pb.AnimateRate, label2);