当我实例化它的Form时,PictureBox不会在KeyDown上移动

时间:2015-03-13 06:36:12

标签: vb.net

我正在创建一个游戏,如果用户按下键盘上的某个键,则图片框将从特定方向移动。我创建了一个类,当用户按下箭头键时,该类处理Picturebox的移动。这是什么但PictureBox没有向右移动

Public Class movement

    Public Function right() As Action

        Dim myform As New Form3()
        myform.PictureBox1.Left += 1
        Return right
    End Function
End Class

+++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++

Public Class Form3

    Public Sub Form3_KeyDown1(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        Dim r As New movement

        Select Case e.KeyCode

            Case Keys.Right
                r.right()

        End Select
    End Sub


End Class

但是,当我直接在Class运动上调用Form3时,PictureBox从右侧移动。我实例化一个表单的原因因为我将使用一些框架或不同形式的大量图片框。

Public Class movement

    Public Function right() As Action


        Form3.PictureBox1.Left += 1
        Return right
    End Function
End Class

1 个答案:

答案 0 :(得分:0)

最后我为你写了一个例子。

Public Class FormTarget
    Friend Sub LeftMovePictureBox(Optional ByVal offset As Integer = 1)
        PictureBox1.Left += offset
    End Sub
    Friend Sub TopMovePictureBox(Optional ByVal offset As Integer = 1)
        PictureBox1.Top += offset
    End Sub
End Class

控制器:

Public Class FormController
    ReadOnly _formTarget As New FormTarget

    Protected Overrides Sub OnLoad(e As EventArgs)
        _formTarget.Show()
        MyBase.OnLoad(e)
    End Sub

    Protected Overrides Sub OnClosing(e As System.ComponentModel.CancelEventArgs)
        _formTarget.Close()
        MyBase.OnClosing(e)
    End Sub

    Private Sub FormController_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        Dim speed As Double = 1D
        If e.Modifiers = Keys.Control Then
            speed = 10D
        End If
        Select Case e.KeyValue
            Case Keys.Right
                _formTarget.LeftMovePictureBox(1 * speed)
            Case Keys.Left
                _formTarget.LeftMovePictureBox(-1 * speed)
            Case Keys.Up
                _formTarget.TopMovePictureBox(-1 * speed)
            Case Keys.Down
                _formTarget.TopMovePictureBox(1 * speed)
        End Select
    End Sub
End Class

我希望它有所帮助。