Results 1 to 6 of 6

Thread: [RESOLVED] How to make a continueous button

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] How to make a continueous button

    I need a button that when I hold it down it will cycle through the loop repeatetly until I release it.

    Code:
    Private Sub cmdMovePanelLeft()
      '
      ' <--- what to add here
      '
     picPanel.Left = picPanel.Left + 1
      '
      ' <-- what to add here
      '
    End Sub


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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to make a continueous button

    One method is to use a timer.

    1. In the button's MouseDown event, test for Left button, and if so, set timer interval and enable timer
    2. In the button's MouseUp event, test for Left button, and if so, disable timer
    3. In the timer event, move your panel by 1 pixel or twip as appropriate for scalemode used

    Edited: Probably should perform similar panel moves during a button's click event too since those events are fired continuously while button has focus and Enter key is held down. If you do handle the click event, know that the mouse button being released can fire a click event too. In this case, you probably don't want to move the panel 1 extra time due to a click event occurring (the timer has been moving it while the button was down). I'd probably handle the situation by using a form-level boolean value (or the Tag property of the button). In the MouseDown event set that boolean/Tag value. In the click event, test if the boolean/Tag is set & if so, don't move the panel. If not set, then user clicked button with keyboard instead of mouse. In the MouseUp event reset that boolean/Tag value. Just an idea.

    ^^ Better yet. Just test to see if the Timer is enabled vs. using some extraneous value
    Last edited by LaVolpe; Jul 25th, 2011 at 02:53 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: How to make a continueous button

    jms, as lavolpe said, something like this, this just add / subtract the value while pressing button, just add 2 cmd button, 1 label, 1 timer
    Code:
    Dim Cnt As Integer, Cmd As Integer
    
    Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        Cmd = 1
        Timer1.Enabled = True
    End If
    End Sub
    
    Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        Timer1.Enabled = False
    End If
    End Sub
    
    Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        Cmd = 2
        Timer1.Enabled = True
    End If
    End Sub
    
    Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        Timer1.Enabled = False
    End If
    End Sub
    
    Private Sub Form_Load()
    Timer1.Interval = 10
    Timer1.Enabled = False
    End Sub
    
    Private Sub Timer1_Timer()
    If Cmd = 1 Then
        Cnt = Cnt - 1
    ElseIf Cmd = 2 Then
        Cnt = Cnt + 1
    End If
    Label1.Caption = Cnt
    End Sub
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to make a continueous button

    I didn't see a purpose in using a timer. What could that do that the below can't?:

    Code:
    Dim KeepDoingItAs Boolean
    
    Private Sub cmdMovePanelLeft_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       KeepDoingIt = True
     
       Do While KeepDoingIt
         DoEvents
         picPanel.Left = picPanel.Left + 1
       Loop
     End If
    End Sub
    
    Private Sub cmdMovePanelLeft_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     KeepDoingIt = False
    End Sub


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

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to make a continueous button

    A timer would keep the speed constant and controllable.

    Without it the speed can vary wildly, and can be far too fast depending on circumstances (such as CPU speed, and other programs running, etc).

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to make a continueous button

    OK, I'm sold. So, Ill use the timer approach.

    Thanks, everyone


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

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