【问题标题】:I need help with UpdateForm(); (Windows Forms Aplication)我需要 Update From() 方面的帮助; (Windows 窗体应用程序)
【发布时间】:2012-10-26 01:36:40
【问题描述】:

嘿,我不知道如何创建用于更新表单的对象。 (Windows 窗体应用程序)。我正在根据一本书做一个项目,该项目需要我制作一个赛狗程序。 我需要更新狗的图片框,以便它们移动。感谢您的帮助!

【问题讨论】:

  • 为什么狗要参加赛马项目?
  • 您是在一个 PictureBox 中展示所有狗,还是在每个 PictureBox 中展示不同的狗?
  • 所有的狗都以随机的速度移动。所以我想他们都需要立即更新他们的位置,以便它是实时的。
  • 哦。每只狗都在不同的图片框中。

标签: c# forms


【解决方案1】:

执行此操作的简单方法是按照以下步骤操作:

  1. 将对象添加到您的 System.Windows.Forms.Timer 表单中
  2. 设置它的间隔。
  3. 将其设置为启用。
  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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2011-08-06
    • 2011-04-27
    • 1970-01-01
    相关资源
    最近更新 更多