简体   繁体   English

将PictureBox添加到另一个类的控件中

[英]Add PictureBox to Controls From Another Class

I've made a class for a creature that is supposed to create a picture box on the form. 我为应该在表单上创建图片框的生物创建了一个类。 My problem is that I want to make a method that creates the picture in my class and also a method for moving it, but I can't access the controls from my class. 我的问题是我想创建一个在类中创建图片的方法以及一个用于移动图片的方法,但是我无法从类中访问控件。 I tried using Controls.Add(MyPicture); 我尝试使用Controls.Add(MyPicture); on the Form.cs, but then I can't remove the picture box from the controls to move it. 在Form.cs上,但随后我无法从控件中删除图片框以将其移动。 Any help would be great. 任何帮助都会很棒。 Thanks. 谢谢。

If you're making a game using WinForms, then simply don't as they're not meant for that kind of purpose. 如果您要使用WinForms制作游戏,那么就不要这么做了,因为它们并非旨在达到这种目的。 There are libraries aimed at that (XNA and the multiplatform Monogame are the most popular). 有针对此的库(XNA和多平台Monogame最受欢迎)。

Howerver, to answer your question, your problem is that you need an instance to the Form1 class, so that you'll be able to call myFormInstance.Controls.Add / Remove at will. 但是,要回答您的问题,您的问题是您需要Form1类的实例,以便能够随意调用myFormInstance.Controls.Add / Remove。 Now, to get your instance there's two ways. 现在,有两种方法可以获取实例。 The simplest one is to check your Program.cs code. 最简单的方法是检查您的Program.cs代码。 You'll see that a Form1 gets instantiated. 您将看到Form1被实例化。 That "new Form1()" is the code you need to globalize. “ new Form1()”是您全球化的代码。 So, you either transform it in something that looks like the following, and use that static reference from your class, or you follow the Singleton pattern. 因此,您可以将其转换为类似于以下内容的形式,并使用类中的静态引用,或者遵循Singleton模式。

static class Program
{
    public static Form1 myFormInstance;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run((myFormInstance= new Form1()));
    }
}
static class MyClass
{
    static void DoWorkWithForm()
    {
          Program.myFormInstance.Controls.Add(/**/);
    }
}

The singleton pattern is slightly more complicated and I wouldn't suggest it to a beginner, as it may lead to confusion. 单例模式稍微复杂一些,我不建议初学者使用,因为它可能会引起混乱。 If however, you wish to learn about it, check this article . 但是,如果您想了解它,请查看本文

EDIT: After reading better, you also have troubles with moving controls within the form. 编辑:更好地阅读之后,您还存在在窗体内移动控件的麻烦。 First, you don't need to add and remove them every time you need to change a property. 首先,您不需要在每次需要更改属性时都添加和删除它们。 "Form.Controls" is a dynamic list of objects deriving from Control, which is not a value type. “ Form.Controls”是从Control(不是值类型)派生的对象的动态列表。 This means that once you add them, you store a reference to them , rather than a copy. 这意味着,一旦添加它们,便会存储对它们引用 ,而不是副本。 In simple words, modifying them from outside the list is enough to reflect the change everywhere else in your code. 简而言之,从列表外部进行修改就足以在代码的其他地方反映出更改。 Add them just during initialization, and then forget about it. 仅在初始化期间添加它们,然后再忽略它。

To move them, you can follow the same scheme as earlier and make the controls static (ex. public static ImagePanel myImagePanel..), or make them member of the Form1 instance, and work on them through it. 要移动它们,可以采用与之前相同的方案,并使控件成为静态控件(例如,公共静态ImagePanel myImagePanel ..),或者使它们成为Form1实例的成员,并通过它进行处理。 (myFormInstance.myImagePanel rather than Form1.myImagePanel). (myFormInstance.myImagePanel而不是Form1.myImagePanel)。 The latter is the better option. 后者是更好的选择。

In any case, you generally don't want to directly act on the, erm, gears of the machine. 无论如何,您通常都不希望直接对机器的齿轮起作用。 You want to abstract your code so that it looks more like an easy to use interface: if your class tells the form that "it should update the picturebox's position", without actually doing it, you'll have a way better code. 您想对代码进行抽象处理,使其看起来更像一个易于使用的接口:如果您的类告诉表单“它应该更新图片框的位置”,而实际上却没有这样做,那么您将获得一种更好的代码。 This is beneficial in many ways, especially for mantainance. 这在许多方面都是有益的,尤其是对于维护。 An example would be the following. 一个例子如下。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void MovePicturebox(Point p)
    {
        pictureBox1.Location = p;
    }
}

public static class MyClass
{
    public static void DoWork()
    {
        //... code
        Program.myForm.MovePicturebox(new Point(10, 10));
        //... code
    }
}

Try this where you want to add your picturebox 在您要添加图片框的地方尝试此操作

PictureBox pb = new PictureBox();
pb.Width = 200;
pb.Height = 300;
pb.Location = new Point(100, 100);
pb.Visible = true;
pb.Show();
Controls.Add(pb);
pb.BackColor = Color.Black;

You can make your pb a global variable by adding it at the top of your code. 您可以通过在代码顶部添加pb使其成为全局变量。 Then you can access it from anywhere you want and you can edit it. 然后,您可以从任何位置访问它,也可以对其进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM