2010-08-02 80 views
0

嘿,我不能完全弄清楚如何创建更新窗体的对象。 (Windows窗体应用程序)。我正在从一本需要我制作狗赛车计划的书中做一个项目。 我需要更新狗的图片框,以便它们移动。谢谢您的帮助!我需要帮助UpdateForm(); (Windows窗体应用程序)

+1

候选人WQOSO? – 2010-08-02 03:39:55

+1

为何赛狗参加赛马计划? – 2010-08-02 03:43:54

+0

你是在一个PictureBox中显示*所有的*狗还是在每个PictureBox中显示不同的狗? – kbrimington 2010-08-02 03:57:40

回答

2

一个简单的方法做,这是请按照下列步骤操作:

  1. 对象添加到您的System.Windows.Forms.Timer
  2. 集的形式,它的时间间隔。
  3. 将其设置为true。
  4. 创建一个响应Tick事件的事件处理程序。

在事件处理程序中,您可以执行图片框的移动。您可能需要为每个图片框存储一个随机数以获得移动速度。您还需要一种方法来限制盒子可以移动的格式。

这里的以代码的形式概念证明:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     _rate = new Random().Next(1, 10); 

     _timer = new Timer() { Interval = 100, Enabled = true }; 
     _timer.Tick += new EventHandler(timer_Tick); 
    } 

    void timer_Tick(object sender, EventArgs e) 
    { 
     if (this.pictureBox1.Location.X > (this.Size.Width - this.pictureBox1.Size.Width)) 
     { 
      return; 
     } 

     Point newLocation = this.pictureBox1.Location; 
     newLocation.X += _rate; 
     this.pictureBox1.Location = newLocation; 
    } 

    private int _rate; 
    private Timer _timer; 
} 

相关问题