VB 2008对象库和迁移的地方 [英] VB 2008 object stanf and the move from the place

查看:110
本文介绍了VB 2008对象库和迁移的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

this is my code

Private Sub Unitsender_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Unitsender.Tick
       Dim TheSquare As New PictureBox 'the box/unit
       TheSquare.BackColor = Color.Red 'the units
       TheSquare.Width = 15
       TheSquare.Height = 20
       TheSquare.Visible = True
       TheSquare.Location = New Point(Unitmaker.Location.X, Unitmaker.Location.Y - 10)
       TheSquare.Location = New Point(TheSquare.Location.X, TheSquare.Location.Y - 10)
       Panel1.Controls.Add(TheSquare)
   End Sub



现在这就是要发生的事情.当计时器启动时,picurebox会在另一张图片上加载,但是制成的picurbox会向前移动,将其视为太空入侵者游戏



now this is what''s ment to happen. when the timer starts the picurebox will load at this other picture but then the picurbox that was made will move foward, think of this as like a space invader game

推荐答案

From在问题中给出的要求看来,single square必须在计时器的Tick 事件中移动.但是从问题给出的代码中可以看出,每次Tick 事件触发时都会实例化一个新的TheSquare PictureBox ,并且将此新的PictureBox 添加到Panel1中,这使得Panel1 杂乱无章的图片框此外,从代码
From the requirement given in the question it appears that a single square has to move in the Tick event of the timer. But as seen from the code given in the question, a new TheSquare PictureBox is instantiated each time the Tick event fires and this new PictureBox is being added to the Panel1, which makes the Panel1 to be cluttered with so many picture boxes. Further, from the code
TheSquare.Location = New Point(Unitmaker.Location.X, Unitmaker.Location.Y - 10)
TheSquare.Location = New Point(TheSquare.Location.X, TheSquare.Location.Y - 10)


第二行是多余的,因为TheSquare 的位置仅通过递减Y值来重新分配,这可以在第一条语句本身中通过给出Y - 20来实现.

因此,我认为可以在Class 级别上创建PictureBox ,并且可以通过在Tick 事件中分配新位置来移动它.
以下示例代码可能会有所帮助


the second line is redundant, as the Location of TheSquare is reassigned by only decrementing the Y value, which could be achieved in the first statement itself by giving Y - 20.

So, I think a PictureBox can be created at Class level and it may be moved by assigning new location in the Tick event.
The following sample code may be helpful

Public Class Form1
    Dim TheSquare As New PictureBox()
    Dim WithEvents UnitSender As New Timer()
    Dim Panel1 As New Panel

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TheSquare.Size = New Size(15, 15)
        Panel1.Dock = DockStyle.Fill
        TheSquare.BackColor = Color.Red
        Panel1.Controls.Add(TheSquare)
        Controls.Add(Panel1)
        UnitSender.Enabled = True
        Me.Size = New Size(100, 125)
    End Sub

    Private Sub UnitSender_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles UnitSender.Tick
        Dim newX As Integer = TheSquare.Location.X
        Dim newY As Integer = TheSquare.Location.Y + 10
        If newY > (Panel1.Height - TheSquare.Height) Then
            newY = 0
            newX += 10
            If newX > (Panel1.Width - TheSquare.Width) Then
                newX = 0
            End If
        End If
        TheSquare.Location = New Point(newX, newY)
    End Sub
End Class


要运行该示例,请创建一个Windows Forms application,用上面的代码替换Form1.vb文件的内容,然后运行该应用程序.


To run the sample, create a Windows Forms application, replace the contents of the Form1.vb file with the above code and run the application.


这篇关于VB 2008对象库和迁移的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆