0

So I've been working on a little game project called economy, and I've ran into a little problem. What I want is to draw a stick man on the screen when someone hires a new employee, I do this by creating a new picture box, and assigning the stick man image to it. However because I needed to do this over and over again (so that they could have unlimited employees) I had it create a new object each time.

To remove the stick men, when someone fired them, I added a new picture box over the old stick men that is just white, so it looks as if they disappear. However for some reason BringToFront(); does not appear to work with it, and so I can't get the white picture to be drawn over the stick men.

If someone could tell me why it is not working, or a way to reference the original stickmen pictureboxe's to change their image, that would be fantastic.

public void Hire_Worker_Click(object sender, EventArgs e)
{
    workers++;
    money -= 10000;

    PictureBox WPB = new PictureBox();
    //Set location and size of new picturebox
    WPB.Image = Economy.Properties.Resources.Worker;
    WPB.Width = 68;
    WPB.Height = 118;
    WPB.Location = new Point(workerx,400);
    workerx += 68;
    Controls.Add(WPB);
}

public void Fire_Worker_Click(object sender, EventArgs e)
{
    if (workers > 1)
    {
        clickfire++;
        workers--;
        money -= 100;

        if (clickfire == 1)
        {
            workerx -= 68;
        }

        PictureBox WWPB = new PictureBox();
        //Set location and size of picturebox
        WWPB.BringToFront();
        WWPB.Image = Economy.Properties.Resources.whiteworker;
        WWPB.Width = 68;
        WWPB.Height = 118;
        WWPB.Location = new Point(workerx, 375);
        workerx -= 68;
        Controls.Add(WWPB);
    }
}
4

1 回答 1

1

BringToFront 仅在控件已经在父控件中时才有效,因此请移动该行:

Controls.Add(WWPB);
WWPB.BringToFront();
于 2015-03-13T21:05:00.087 回答