PictureBox从表单c#中删除

时间:2016-02-06 16:16:12

标签: c# picturebox

我需要在表单中移动一个图片框。图片框是Fantasma1。

这是我在计时器(移动图片框)时所做的事情:

private void Move_Tick(object sender, EventArgs e)
    {
        Completamento.Increment(1); 

        while (Fantasma1.Location.X < MAX.X)
              Fantasma1.Location = new System.Drawing.Point(Fantasma1.Location.X + 1, Fantasma1.Location.Y);

        Move.Stop();

        MessageBox.Show("Location: " + Fantasma1.Location.X + ";" + Fantasma1.Location.Y + ", larghezza del form: " + this.Width + ", dimensione della picture: " + Fantasma1.Width);
    }

在我完成的表单的构造函数中:

MAX.X = this.Width - Fantasma1.Size.Width;

我认为这个解决方案是正确的,但我的图片框从表单中消失了。 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

首先,你的Fantasma会在没有你看到它的情况下运行到最后:

while (Fantasma1.Location.X < MAX.X)
          Fantasma1.Location = new System.Drawing.Point(Fantasma1.Location.X + 1, Fantasma1.Location.Y);

那个循环将立即移动你的幽灵而没有任何明显的变化,我认为你想要的是通过Move_Tick调用移动一个像素,如果它达到极限然后停止计时器,你只需将计时器停在第一个滴答,另一个错误。

所以,如果你想要一个鬼在每个刻度上移动一个像素并且没有离开屏幕那么这应该可以解决这个问题:

private void Move_Tick(object sender, EventArgs e)
{
    int max = this.Width - Fantasma1.Width;

    if(Fantasma.Left < max)
        Fantasma1.Left++;
    else
        Move.Stop();
}

在你的情况下,测试最大位置的方法是正确的,但我几乎肯定你在图片框加载图片之前试图获得大小(并设置自动调整大小以允许图片框调整它的大小为了避免错误,我在公式上添加了最大计算,它是一个减法,它不消耗任何*过程。

*:几乎任何一行代码都会花费cpu,但减法可能不到一纳秒,只是不明显。