[RESOLVED] VB 6 Picture Box Movement-VBForums
Results 1 to 14 of 14

Thread: [RESOLVED] VB 6 Picture Box Movement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    27

    Resolved [RESOLVED] VB 6 Picture Box Movement

    hey im a new guy,

    Does anyone know how to code movement of 2 pictureboxes simultaneously in VB6? I know how to make the pictureboxes move via key press but i can't move them at the same time.

    Thanks

    AdvComp

  2. #2
    Lively Member
    Join Date
    Oct 2013
    Posts
    63

    Re: VB 6 Picture Box Movement

    Code:
    Private Sub Command1_Click()
    
    Picture1.Left = Picture1.Left + 50
    Picture2.Left = Picture2.Left - 50
    
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    27

    Re: VB 6 Picture Box Movement

    Thanks, but I've already got that part except I'm using
    .Top since I'm moving picturebox up and down. I can code two pictureboxes moving except if you are moving one the other doesn't move. Does anyone know how to solve this?

  4. #4
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    437

    Re: VB 6 Picture Box Movement

    Quote Originally Posted by AdvComp View Post
    I can code two pictureboxes moving except if you are moving one the other doesn't move.
    Can you explain what do you mean ? what code do you use ?

  5. #5
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,765

    Re: VB 6 Picture Box Movement

    Thanks, but I've already got that part except I'm using
    .Top since I'm moving picturebox up and down. I can code two pictureboxes moving except if you are moving one the other doesn't move. Does anyone know how to solve this?
    try the following way . hope it might help
    Code:
    Option Explicit
    Dim bmove As Boolean
    Private Sub btmove_Click()
    bmove = True
    Picture1.Left = Picture1.Left + 50
    End Sub
    
    Private Sub btmovetop_Click()
    If bmove = False Then
      Picture1.Top = Picture1.Top + 50
    End If
    End Sub

  6. #6
    Lively Member
    Join Date
    Oct 2013
    Posts
    63

    Re: VB 6 Picture Box Movement

    post the movement code here.

    I'm taking a long shot guess here, correct me if im wrong, you're using Keypress to move a picture box like this:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If KeyCode = vbKeyDown Then Picture1.Top = Picture1.Top + 50
    If KeyCode = vbKeyUp Then Picture1.Top = Picture1.Top - 50
    If KeyCode = vbKeyLeft Then Picture1.Left = Picture1.Left - 50
    If KeyCode = vbKeyRight Then Picture1.Left = Picture1.Left + 50
    
    End Sub
    And this causes you not to be able to move the picture box up and left (diagonally) simultaneously for example.

    You may use this code instead:

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer ' Auto Typer Keys
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If GetAsyncKeyState(vbKeyDown) Then Picture1.Top = Picture1.Top + 50
    If GetAsyncKeyState(vbKeyUp) Then Picture1.Top = Picture1.Top - 50
    If GetAsyncKeyState(vbKeyLeft) Then Picture1.Left = Picture1.Left - 50
    If GetAsyncKeyState(vbKeyRight) Then Picture1.Left = Picture1.Left + 50
    
    End Sub
    do post if that indeed was the issue.

    do remember that moving a control over another control \ form, will cause it to flicker.
    you may solve that by using Bitbit (i'll elaborate about it if required).

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    27

    Re: VB 6 Picture Box Movement

    I am coding the game Pong. I have A+Z to go up and down for picture1 and for picture2 I use the arrow keys. Except if I press For example, A and an arrow key, only one of the pictures move

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,493

    Re: VB 6 Picture Box Movement

    A picture is worth a thousand words, if you know what I mean.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    27

    Re: VB 6 Picture Box Movement

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    If KeyCode = vbKeyDown Then Picture1.Top = Picture1.Top + 50
    If KeyCode = vbKeyUp Then Picture1.Top = Picture1.Top - 50
    If KeyCode = vbKeyZ Then Picture2.Left = Picture2.Left + 50
    If KeyCode = vbKeyA Then Picture2.Left = Picture2.Left - 50

    End Sub

  10. #10
    Lively Member
    Join Date
    Oct 2013
    Posts
    63

    Re: VB 6 Picture Box Movement

    Name:  Untitled.jpg
Views: 110
Size:  78.9 KB

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If GetAsyncKeyState(vbKeyDown) Then Label1.Top = Label1.Top + 50
    If GetAsyncKeyState(vbKeyUp) Then Label1.Top = Label1.Top - 50
    
    If GetAsyncKeyState(vbKeyZ) Then Label2.Left = Label2.Left + 50
    If GetAsyncKeyState(vbKeyA) Then Label2.Left = Label2.Left - 50
    
    
    End Sub
    This works simultaneously

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    27

    Re: VB 6 Picture Box Movement

    Quote Originally Posted by stum View Post
    Name:  Untitled.jpg
Views: 110
Size:  78.9 KB

    Code:
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If GetAsyncKeyState(vbKeyDown) Then Label1.Top = Label1.Top + 50
    If GetAsyncKeyState(vbKeyUp) Then Label1.Top = Label1.Top - 50
    
    If GetAsyncKeyState(vbKeyZ) Then Label2.Left = Label2.Left + 50
    If GetAsyncKeyState(vbKeyA) Then Label2.Left = Label2.Left - 50
    
    
    End Sub
    This works simultaneously

    Thanks a lot this works for me. But i am wondering what does the declaration do?

    Thanks Again

  12. #12
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    437

    Re: VB 6 Picture Box Movement

    Good, Tag it as resolved

    the declaration is to declare the api that you are using to get if the key is pressed or not ! you are calling this function which is not native VB so you should declare it

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2013
    Posts
    27

    Re: VB 6 Picture Box Movement

    How do you tag it as resolved?

  14. #14
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,130

    Re: VB 6 Picture Box Movement

    Quote Originally Posted by AdvComp View Post
    How do you tag it as resolved?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0

    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.