我如何使一个类创建一个PictureBox对象内的形式
本文关键字:对象 PictureBox 一个 何使一 创建 | 更新日期: 2023-09-27 18:16:08
我试图创建一个MainCharacter
类,其工作是根据每次加载时通过房间传递的一些参数在"房间"对象内创建PictureBox
。
下面是MainCharacter
类的代码:
namespace VirtualMuseum
{
class MainCharacter
{
string characterName;
int characterGender;
bool registeredUser;
int[] playerPosition;
// Character constructor
public MainCharacter(string name, int gender, bool registered, int[] location)
{
characterName = name;
characterGender = gender;
registeredUser = registered;
playerPosition = location;
}
public void drawCharacter()
{
PictureBox playerBox = new PictureBox();
playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx;
playerBox.Width = 28;
playerBox.Height = 32;
playerBox.Location = new Point(playerPosition[0], playerPosition[1]);
playerBox.Visible = true;
}
}
}
以及在room1中创建播放器对象的代码行,例如
MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
问题是当进入创建播放器对象的特定房间时,没有PictureBox
在表单内可见。
-----新增动作-----
根据您的指示,在Room类
中使用以下代码public Hall()
{
playerPosition = new int[] { 350, 400 };
InitializeComponent();
//pictureBox2.Parent = pictureBox1;
MessageBox.Show(playerPosition.ToString());
MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
player1.drawCharacter(this);
}
和MainCharacter类中的以下代码:
public void drawCharacter(Form form)
{
PictureBox playerBox = new PictureBox();
playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx;
playerBox.Width = 28;
playerBox.Height = 32;
playerBox.Location = new Point(playerPosition[0], playerPosition[1]);
// Add the pictureBox to the selected form
form.Controls.Add(playerBox);
}
我设法在表单中绘制一些东西,但它似乎是一条非常小的线,尽管我在drawCharacter方法中定义了picturebox的大小。
Option1
有一个窗体的实例,在drawCharacter:
formInstance.Controls.Add(playerBox);
例如:public void drawCharacter(Form formInstance)
{
PictureBox playerBox = new PictureBox();
// Set properties ...
formInstance.Controls.Add(playerBox);
}
然后在表单中,当你需要调用这个方法时使用:
var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
player1.drawCharacter(this);
Option2
你可以改变你的方法来返回一个PictureBox
:
public PictureBox drawCharacter()
{
PictureBox playerBox = new PictureBox();
// Set properties ...
return playerBox;
}
然后在表单中,当你需要调用这个方法时使用:
var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
this.Controls.Add(player1.drawCharacter());
关键字:
- 正如"Ivan Stoev"在评论中提到的,所有控件必须添加到
Controls
集合的形式。 不要忘记将位置属性设置为合适的位置或更好的选项,使用FlowLayoutPanel并将您的PictureBox添加到其中。例如,如果你把一个FlowLayoutPabel从(flowLayoutPanel1),你可以使用选项2,并以这种方式添加图片框
var player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
this.flowLayoutPanel1.Controls.Add(player1.drawCharacter());
您忘记将PictureBox
添加到表单中。没有表格来安置你的PictureBox
将不会出现。您的代码可能需要将表单传递到drawCharacter()
方法中,并在该方法中添加如下内容:
YourFormVariable.Controls.Add(playberBox)
作为旁注:您不希望一遍又一遍地向表单添加PictureBox
,您希望添加一个并不断操作它。我只提到这是一个警告,因为drawCharacter()
听起来像它可以在"渲染"类型循环中调用。
问题是,你不传递你的表单实例到你的类,所以它不能创建控制。将drawCharacter
方法更改为:
class MainCharacter
{
string characterName;
int characterGender;
bool registeredUser;
int[] playerPosition;
// Character constructor
public MainCharacter(string name, int gender, bool registered, int[] location)
{
characterName = name;
characterGender = gender;
registeredUser = registered;
playerPosition = location;
}
public void drawCharacter(Form form)
{
PictureBox playerBox = new PictureBox();
playerBox.Image = Properties.Resources.mc___main_characters_sprites_by_ssb_fan4ever_d53kkhx;
playerBox.Width = 28;
playerBox.Height = 32;
playerBox.Location = new Point(playerPosition[0], playerPosition[1]);
playerBox.Visible = true;
form.Controls.Add(playerBox);
}
}
并像这样使用:
MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
player1.drawCharacter(this);