Subroutines in the vb6

Asked

Viewed 301 times

0

I have a form and added to it , a button and an image ,I wonder if it is possible to create a subroutine where by clicking the button the image moves from one side to the other? if it is possible as it is in . vb6 Since the graduation the help of you hugs

  • 1

    Instead of deleting your questions, improve them by following the tips given in the comments. After the person updates and improves the post, we can reverse the negative vote that was given.

1 answer

1

It is possible to do this by creating a Timer in his Form, as shown in the figure below:

Timer no Form

Then just adjust the properties Interval and Enabled of Timer created, to be able to create a repetitive event (where you will make your PictureBox move), as in this code:

Option Explicit

Private Indo As Boolean

Private Sub Form_Load()
    'Inicialmente, desliga o timer, e configura seu intervalo
    '(quanto menor, mais rápido)
    Timer1.Enabled = False
    Timer1.Interval = 10
End Sub

Private Sub Command1_Click()
    If Timer1.Enabled = True Then
        Timer1.Enabled = False
    Else
        Timer1.Enabled = True
    End If
End Sub

Private Sub Timer1_Timer()
    '10 é um valor arbitrário (quanto maior, mais vai se mover)
    If Indo = True Then
        Picture1.Left = Picture1.Left + 10
        'Se bateu no lado direito, volta
        If Picture1.Left >= ScaleWidth - Picture1.Width Then
            Indo = False
            Picture1.Left = ScaleWidth - Picture1.Width
        End If
    Else
        Picture1.Left = Picture1.Left - 10
        'Se bateu no lado esquero, vai de novo
        If Picture1.Left <= 0 Then
            Indo = True
            Picture1.Left = 0
        End If
    End If
End Sub

Unused Timer, as requested (not best practice):

Private Sub Command1_Click()
    Do While True
        If Indo = True Then
            Picture1.Left = Picture1.Left + 10
            'Se bateu no lado direito, volta
           If Picture1.Left >= ScaleWidth - Picture1.Width Then
                Indo = False
                Picture1.Left = ScaleWidth - Picture1.Width
                Exit Do
            End If
        Else
            Picture1.Left = Picture1.Left - 10
            'Se bateu no lado esquero, vai de novo
           If Picture1.Left <= 0 Then
                Indo = True
                Picture1.Left = 0
                Exit Do
            End If
        End If
        DoEvents
    Loop
End Sub

With 4 buttons, one for each direction as requested:

Option Explicit

Private dir As Long

Private Sub cmdUp_Click()
    dir = 0
    Timer1.Enabled = True
End Sub

Private Sub cmdLeft_Click()
    dir = 1
    Timer1.Enabled = True
End Sub

Private Sub cmdDown_Click()
    dir = 2
    Timer1.Enabled = True
End Sub

Private Sub cmdRight_Click()
    dir = 3
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Select Case dir
    Case 0
        Picture1.Top = Picture1.Top - 10
    Case 1
        Picture1.Left = Picture1.Left - 10
    Case 2
        Picture1.Top = Picture1.Top + 10
    Case 3
        Picture1.Left = Picture1.Left + 10
    End Select
End Sub
  • it is possible to do this without the timer using a cycle?

  • Hi @user7752. You can do this without the Timer. Just use a While, For, or any other loop structure you want, placing at the end of the loop the command DoEvents, not to "lock" the window. But this solution is not very advisable ;)

  • I tried with the command Doeventes gave the first click moves only once , it does not move until I click another button and I wanted that when giving a click it moves on the form, there is no other effective way to do this?

  • Hi @user7752. I made a version (added in the reply). But honestly, it gets so, so fast that you won’t see the animation. It really is just for you to kill your curiosity, because it is a bad technique in everyday applications ;)

  • The image only moves to a single side and the process is very fast There is no way to be a little slower as a vlc music player does for example when playing how it moves.

  • Where can I find good vb6 manuals?

  • Hi @user7752... So it’s fast, really! To slow down, you would have to do the Thread sleep, which would ruin the response of the UI (window). So I said it was bad practice, and had to use the Timer ;) Now manuals... I don’t know, no :(

  • Buddy has no way to make the image go around the form non-stop (as it is now, without the timer)? I tried but it did not work no.

  • @user7752 ... then without the Timer, in VB6 does not. There are other techniques, but they are more complicated than the Timer of the VB6.

  • You don’t have any code or something related to a cycle using a FOR or other decision box capable of doing this?

  • @user7752 So it is not a question of the type of loop. Do, While, For are similar. The question goes deeper than that. It’s a problem of Thread and processing. Or makes a Timer, or makes creating another Thread (directly or indirectly). Any other way will destroy with the response of your program.

  • Sorry Carlos and with the timer as it would be?

  • @user7752 Looks like the first code I posted in the reply ;)

  • Option Compare Database Private Sub Command0_click() Do Until Me.Width < 4000 Let Image1.Left = Image1.Left - 5 Doevents Loop Without stopping with a button and using the timer? Does anyone have an effective idea of how to do this?

  • @user7752 ... Sorry, besides the three forms of the answer, without using other external components, I do not know how else to do. Just remember, lock on one loop/while/for with a DoEvents ultimately works, but it’s not the best option for a real application. There’s a reason you don’t want to use a Timer?

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.