单击VB.Net时如何移动多个图片框?

时间:2015-05-18 08:41:38

标签: vb.net visual-studio-2010

我将在表单上有30个不同的图片框,我使用键盘上的箭头键移动它们。但是我不知道如何使用我的代码移动多个图片框。理想情况下,我想在点击时一次移动一个,这是我必须移动图片框的代码:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    Select Case e.KeyCode
        Case Keys.Left 'Left Arrow Key to move picture boxes left
            Me.PictureBox1.Left -= 1
        Case Keys.Up 'Up Arrow Key to move picture boxes left
            Me.PictureBox1.Top -= 1
        Case Keys.Right 'Right Arrow Key to move picture boxes left
            Me.PictureBox1.Left += 1
        Case Keys.Down 'Down Arrow Key to move picture boxes left
            Me.PictureBox1.Top += 1
    End Select
End Sub

单击时如何单独移动30个图片框中的一个?

感谢您的时间

1 个答案:

答案 0 :(得分:0)

您是PictureBox类型的班级变量 此变量应该包含对用户单击的最后一个PicrureBox的引用。 然后,您所要做的就是更改已经移动类级别变量的KeyDown代码。

' on the form level
dim CurrentPictureBox As PictureBox

' use the same Click event for all PictureBoxes to populate this variable:
CurrentPictureBox = Cast(Sender, PictureBox)


' Change your existing code:
if not CurrentPictureBox is nothing then
Select Case e.KeyCode
    Case Keys.Left 'Left Arrow Key to move picture boxes left
        CurrentPictureBox.Left -= 1
    Case Keys.Up 'Up Arrow Key to move picture boxes left
        CurrentPictureBox .Top -= 1
    Case Keys.Right 'Right Arrow Key to move picture boxes left
        CurrentPictureBox .Left += 1
    Case Keys.Down 'Down Arrow Key to move picture boxes left
        CurrentPictureBox.Top += 1
End Select
end if 

注意:直接写在这里的代码,自从我的VB.Net日起,它已经有一段时间了。代码中可能存在一些错误,但这个想法很重要。