C# 让另一个类控制主窗体上的图片[]
我尝试为赛车创建一个类bgcar,负责处理与某些赛车相关的所有变量,其中一个变量是赛车的图片,放在主窗体上。所有变量速度位置汽油等工作,但我从下面删除 然而,加载和更新图像结果是有问题的。 我尝试了下面代码的一些变体,但到目前为止没有任何效果 我可以想象有人会想,为什么不简单地把它全部写在主窗体中,但是我不想创建很多窗体变量,我希望一个类能够自己处理它,并使用classtype.update。更新主窗体。 Wich是一个winform应用程序C# 让另一个类控制主窗体上的图片[],c#,winforms,class,C#,Winforms,Class,我尝试为赛车创建一个类bgcar,负责处理与某些赛车相关的所有变量,其中一个变量是赛车的图片,放在主窗体上。所有变量速度位置汽油等工作,但我从下面删除 然而,加载和更新图像结果是有问题的。 我尝试了下面代码的一些变体,但到目前为止没有任何效果 我可以想象有人会想,为什么不简单地把它全部写在主窗体中,但是我不想创建很多窗体变量,我希望一个类能够自己处理它,并使用classtype.update。更新主窗体。 Wich是一个winform应用程序 namespace game { public c
namespace game
{
public class Main:Form
{
bgcar racecars;
public Main()
{
racecars = new bgcar();
racecars.update
//to add later
//traficlights.update
//people.update
}
}
class bgcar
{
public PictureBox[] wagon;
public bgcar() //constructor
{
wagon = new PictureBox[2];
//below wont work.
//while on the main form
// picturebox1.image new bitmap(game.Properties.Resources.SportwagonA)
//works, proving it does exist in the resource file.
wagon[0].image = new Bitmap(game.Properties.Resources.SportwagonA);
wagon[1].image = new Bitmap(game.Properties.Resources.SportwagonB);
}
public void update()
{
//displaying those images is also a problem
Random rnd = new Random();
int x = rnd.next(100);
int y = rnd.next(100);
Wagon[0].location = new point (x,y);
int x = rnd.next(100);
int y = rnd.next(100);
Wagon[1].location = new point (x,y);
}
}
}
==更新
先吃吧,我现在正考虑在我的主要形式上创造
一个公共图片盒[]跑车,然后在我的主窗体中有一个例程
执行类似以下操作:sportscar=bgcar.wage;不确定这是否有效。将picturebox数组定义为静态
然后,您可以使用其他类的类名来访问它,如
public class Main:Form
{
public Main()
{
bgcar.wagoon // and something happens
}
}
这就是我所看到的
所以你的racecars类在你使用它的方式上基本上是无用的。每次创建一个新的PictureBox实例都不会给您带来任何好处。您必须将它们添加到表单或其他父控件中,以便将它们绘制到其中
racecars = new bgcar();
racecars.update();
foreach(PictureBox pBox in racecars.wagon)
{
this.Controls.Add(pBox);
}
将表单传递到类中,以便可以向其中添加控件:
public Main()
{
racecars = new bgcar(this); // <--- Pass the Form in
racecars.update();
}
以下是该类的更新版本:
}
public class bgcar
{
private Form frm;
private List<PictureBox> wagon;
private Random rnd = new Random();
public bgcar(Form frm) //constructor receives the Form
{
this.frm = frm; // store Form reference for possible later use
wagon = new List<PictureBox>();
PictureBox pb = new PictureBox();
pb.Image = new Bitmap(game.Properties.Resources.SportwagonA);
frm.Controls.Add(pb); // add created control to both the Form and our List
wagon.Add(pb);
pb = new PictureBox();
pb.Image = new Bitmap(game.Properties.Resources.SportwagonB);
frm.Controls.Add(pb);
wagon.Add(pb);
}
public void update()
{
foreach(PictureBox pb in wagon)
{
int x = rnd.Next(100);
int y = rnd.Next(100);
pb.Location = new Point(x, y);
}
}
}
在类bgcar内部,我公开了静态PictureBox[],但它不起作用?我只是想表明,您可以通过bgcar类访问主类中的PictureBox数组。我只想在主类中对其进行一次实例化,然后调用类上的racecars.update方法来更新其所有设置并调整其位置。你的foreach会继续添加图片吗?现在你只是在创建对象。您必须将它们附加到某个父控件或窗体,以便在屏幕上绘制它们。此代码确保在更新调用后将它们添加到绘图中。我希望通过调用更新使bgcar类图像数组[]和控件处于更新状态,以便它在主窗体上显示图像。目前它由两部分组成:基于资源文件的图片分配和将图片更新为主窗体。您的方法不同,它将图片从bgcar类中提取出来。但我的错误发生在我的类中没有加载早期图像,我不确定main是否应该从bgclass中提取此内容,这是另一种方式。哦,我会尝试,但可能是明天晚些时候,我一直在尝试让它更早地工作,但一定是打错了什么,我希望这是正确的方法。我在racecar.addpb中得到一个system.NullReferenceException,这很奇怪,因为它前面的行frm.Controls.addpb工作正常,但不确定为什么,我在类中创建了一个数组而不是列表。数组指向同一个PB,我可以用PB来改变位置,如果你想它就像是在C++中访问指针,但是现在它工作了,那就有点奇怪了。
}
public class bgcar
{
private Form frm;
private List<PictureBox> wagon;
private Random rnd = new Random();
public bgcar(Form frm) //constructor receives the Form
{
this.frm = frm; // store Form reference for possible later use
wagon = new List<PictureBox>();
PictureBox pb = new PictureBox();
pb.Image = new Bitmap(game.Properties.Resources.SportwagonA);
frm.Controls.Add(pb); // add created control to both the Form and our List
wagon.Add(pb);
pb = new PictureBox();
pb.Image = new Bitmap(game.Properties.Resources.SportwagonB);
frm.Controls.Add(pb);
wagon.Add(pb);
}
public void update()
{
foreach(PictureBox pb in wagon)
{
int x = rnd.Next(100);
int y = rnd.Next(100);
pb.Location = new Point(x, y);
}
}
}