我正在嘗試創建一個MainCharacter
班,其任務是根據每個通過房間傳遞的參數在「房間」對象內創建一個PictureBox
時間加載。我如何讓班級在表格內創建一個PictureBox對象
這裏是MainCharacter
類的代碼:對於示例創建內部房間1選手客體
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;
}
}
}
和代碼行
MainCharacter player1 = new MainCharacter(playerName, playerGender, registeredUser, playerPosition);
的問題是,沒有PictureBox
裏面可見當進入創建玩家對象的特定房間時形成。
-----新動作-----
按照您的指示,使用房類
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的大小,但我設法在窗體內繪製了東西,但它看起來像一條非常小的線。
formInstance.Controls.Add(playerBox);
例如:
的PictureBox(及任何控制)必須被添加到形式,以變得可見。 –