Reputation: 3
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);
on the Form.cs, but then I can’t remove the picture box from the controls to move it. Any help would be great. Thanks.
Upvotes: 0
Views: 1748
Reputation: 671
If you're making a game using WinForms, then simply don't as they're not meant for that kind of purpose. There are libraries aimed at that (XNA and the multiplatform Monogame are the most popular).
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. Now, to get your instance there's two ways. The simplest one is to check your Program.cs code. You'll see that a Form1 gets instantiated. That "new Form1()" is the code you need to globalize. 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.
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. 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. (myFormInstance.myImagePanel rather than 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
}
}
Upvotes: 1
Reputation: 2896
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. Then you can access it from anywhere you want and you can edit it.
Upvotes: 0