1
我有這個課程的魚,我只想初始化8種類型的魚,將它定位在窗體中。當我運行我的代碼時,八條魚永遠不會出現。在某些情況下,一個,兩個或三個。但是當我調試代碼時,出現了八條魚。爲什麼PictureBox不會出現在窗體中之後?
我真的不知道發生了什麼。希望你能幫我找到答案。
class Fish : Aquaticanimal
{
private int x;
public int X
{
get { return x; }
set { x = value; }
}
private int y;
public int Y
{
get { return y; }
set { y = value; }
}
public Fish(Bitmap fish,Form form)
{
quantity++;
aquaticAnimal.BackColor = Color.Transparent;
aquaticAnimal.Image = fish;
aquaticAnimal.SizeMode = PictureBoxSizeMode.StretchImage;
positioning(form);
}
private void positioning(Form form)
{
Random rnd = new Random();
int FormWidth= Convert.ToInt32(form.Size.Width.ToString());
int FormHeight = Convert.ToInt32(form.Size.Height.ToString());
aquaticAnimal.Parent=form;
aquaticAnimal.Top = rnd.Next(200, FormHeight - 100);
aquaticAnimal.Left = rnd.Next(0, FormWidth - 100);
this.x = aquaticAnimal.Top;
this.y = aquaticAnimal.Left;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Bitmap[] images = new Bitmap[8];
images[0] = Pecera.Properties.Resources.Nemo;
images[1] = Pecera.Properties.Resources.Pez_hembra;
images[2] = Pecera.Properties.Resources.Pez_macho;
images[3] = Pecera.Properties.Resources.tiburon_adulto;
images[4] = Pecera.Properties.Resources.Tiburon_bebe_hembra;
images[5] = Pecera.Properties.Resources.tiburon_macho;
images[6] = Pecera.Properties.Resources.Dorys;
images[7] = Pecera.Properties.Resources.tiburon_hembra;
Fish[] fish = new Fish[8];
for (int x = 0; x < 8; x++)
{
fish[x] = new Fish(images[x], this);
}
}
添加到的是,OP不應該'的ToString()'只是轉換回'int'。 – SimpleVar
@Yorye好點。在這段代碼中有很多東西需要修復。 –
也可以閱讀有關自動屬性。 – SimpleVar