Results 1 to 3 of 3

Thread: Moving objects with the arrow keys

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    1

    Exclamation

    My cousin is working on a VB program for school and is wondering how to make it so that when he has "compiled" (I don't know a lot about VB, so I don't know how you go about doing things) the program, he can use the arrow keys to move an object. He really needs to know how to do this fast, because it was due a couple of days ago. Please respond ASAP.

    [email protected]

  2. #2
    Addicted Member
    Join Date
    Jul 1999
    Location
    Portland, OR.
    Posts
    226
    Hi,

    Try:
    In a Keydown event:

    if keycode=VbKeyDown then
    Object.Left= Object.Left + 100
    Object.Top = Object.top + 100
    end if

    You got the idea ??

    Good Luck.


  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    This moves a picturebox around the screen but the same methods can be used to move other controls as well. First set the form's KeyPreview property to True, then add this code:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
        Const NUDGE = 100
        
        Select Case KeyCode
            Case vbKeyRight
                If Picture1.Left + Picture1.Width + NUDGE >= Form1.ScaleWidth Then
                    ' The picture has reached or almost reached the right-hand
                    ' border of the form. Move it only as far as it can go without
                    ' it crossing the border.
                    Picture1.Left = Form1.ScaleWidth - Picture1.Width
                    Beep
                Else
                    Picture1.Left = Picture1.Left + NUDGE
                End If
            Case vbKeyLeft
                If Picture1.Left - NUDGE <= 0 Then
                    Picture1.Left = 0
                    Beep
                Else
                    Picture1.Left = Picture1.Left - NUDGE
                End If
            Case vbKeyUp
                If Picture1.Top - NUDGE <= 0 Then
                    Beep
                    Picture1.Top = 0
                Else
                    Picture1.Top = Picture1.Top - NUDGE
                End If
            Case vbKeyDown
                If Picture1.Top + Picture1.Height + NUDGE >= Form1.ScaleHeight Then
                    Beep
                    Picture1.Top = Form1.ScaleHeight - Picture1.Height
                Else
                    Picture1.Top = Picture1.Top + NUDGE
                End If
        End Select
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width