Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从另一个类VS2010访问Form1.picturebox1_C# - Fatal编程技术网

C# 从另一个类VS2010访问Form1.picturebox1

C# 从另一个类VS2010访问Form1.picturebox1,c#,C#,我是C#的新手 我遇到了一些问题,我正在通过Form.cs[设计]在Form1中创建一些picturebox 我想从Form1访问图片框 所以我必须在另一个类上创建一个对象来访问Form1.picturebox1 但我能做对,有人能帮我吗 namespace RaceGame{ class Greyhound { Form1 runner; public int StartingPosition = 13; public int RacetrackLength = 4

我是C#的新手

我遇到了一些问题,我正在通过Form.cs[设计]在Form1中创建一些picturebox

我想从Form1访问图片框

所以我必须在另一个类上创建一个对象来访问Form1.picturebox1

但我能做对,有人能帮我吗

namespace RaceGame{
class Greyhound
{

    Form1 runner;

    public int StartingPosition = 13;
    public int RacetrackLength = 420;
    public PictureBox MyPictureBox = null;
    public int Location = 0;
    public Random Randomizer;

    public void Run()
    {

        runner.pictureDog1.Location = new Point(13, 100);
    }     
}
这是表格一

namespace RaceGame{public partial class Form1 : Form
{
    Greyhound racing = new Greyhound();
    public Form1()
    {
        InitializeComponent();
    }

    private void raceButton_Click(object sender, EventArgs e)
    {
        racing.Run();
    }



}

}

根据您提供的代码,您的Form1或“runner”未被分配。 你有两个选择- 1) 像这样公开你的跑步者

public Form1 runner

这将允许您(在窗体中,而不是在类中)将窗体分配给此属性

public class Form1
{
        protected void Form1_Load(object sender, EventArgs e)
        {
              Greyhound gh = new Greyhound();
              gh.runner = this;//this line here
              //then you can call gh.Run()
        }
}
另一种选择是使用构造函数,如下所示-

class Greyhound
{

    Form1 runner;

    public int StartingPosition = 13;
    public int RacetrackLength = 420;
    public PictureBox MyPictureBox = null;
    public int Location = 0;
    public Random Randomizer;

    //here
    public Greyhound(Form1 form)
    {
        this.runner = form;
    }
    public void Run()
    {

        runner.pictureDog1.Location = new Point(13, 100);
    }     
}
这将把表单传递到greyhound类中,供您访问,然后在表单代码中-

public class Form1
{
        protected void Form1_Load(object sender, EventArgs e)
        {
              Greyhound gh = new Greyhound(this);//pass the form as 'this'
              //then you can call gh.Run()
        }
}
编辑: 基于您提供的表单代码-

namespace RaceGame
{
    public partial class Form1 : Form
    {
         Greyhound racing = new Greyhound();
         public Form1()
         {
            InitializeComponent();
         }

         private void raceButton_Click(object sender, EventArgs e)
         {
              racing.runner = this;//assign property here
              racing.Run();
         }
    }
}
还有灰狗班

namespace RaceGame
{
    class Greyhound 
    {

        public Form1 runner;//make property public

        public int StartingPosition = 13;
        public int RacetrackLength = 420;
        public PictureBox MyPictureBox = null;
        public int Location = 0;
        public Random Randomizer;

        public void Run()
        {

            runner.pictureDog1.Location = new Point(13, 100);
        }      
    }
}

根据您提供的代码,未分配Form1或“runner”。 你有两个选择- 1) 像这样公开你的跑步者

public Form1 runner

这将允许您(在窗体中,而不是在类中)将窗体分配给此属性

public class Form1
{
        protected void Form1_Load(object sender, EventArgs e)
        {
              Greyhound gh = new Greyhound();
              gh.runner = this;//this line here
              //then you can call gh.Run()
        }
}
另一种选择是使用构造函数,如下所示-

class Greyhound
{

    Form1 runner;

    public int StartingPosition = 13;
    public int RacetrackLength = 420;
    public PictureBox MyPictureBox = null;
    public int Location = 0;
    public Random Randomizer;

    //here
    public Greyhound(Form1 form)
    {
        this.runner = form;
    }
    public void Run()
    {

        runner.pictureDog1.Location = new Point(13, 100);
    }     
}
这将把表单传递到greyhound类中,供您访问,然后在表单代码中-

public class Form1
{
        protected void Form1_Load(object sender, EventArgs e)
        {
              Greyhound gh = new Greyhound(this);//pass the form as 'this'
              //then you can call gh.Run()
        }
}
编辑: 基于您提供的表单代码-

namespace RaceGame
{
    public partial class Form1 : Form
    {
         Greyhound racing = new Greyhound();
         public Form1()
         {
            InitializeComponent();
         }

         private void raceButton_Click(object sender, EventArgs e)
         {
              racing.runner = this;//assign property here
              racing.Run();
         }
    }
}
还有灰狗班

namespace RaceGame
{
    class Greyhound 
    {

        public Form1 runner;//make property public

        public int StartingPosition = 13;
        public int RacetrackLength = 420;
        public PictureBox MyPictureBox = null;
        public int Location = 0;
        public Random Randomizer;

        public void Run()
        {

            runner.pictureDog1.Location = new Point(13, 100);
        }      
    }
}

你能展示一下你的灰狗课程的初始化吗?这是学校的项目吗?!不,我只是想建立一个赛车场,你能展示一下你的灰狗类的初始化吗?这是学校的项目吗?!不,我只是想建立一个赛车游戏