[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Scrolling help

I have an application that displays a map, but the map is too big, so there is a scroll bar on the botton to scroll left and right.  Is there a way I can assign Left and Right buttons to perform the same function as clicking on the arrows?


Thanks
0
sewoo
Asked:
sewoo
  • 4
1 Solution
 
dbrckoviCommented:
Try this:

Before using this, be sure to set the form's keypreview property to true.

--------------------------------------------------------------------------------------
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   
    If KeyCode = vbKeyLeft Then                                           'if left pressed
        If HScroll1.Value > 99 Then                                            'prevent the value to be negative
            HScroll1.Value = HScroll1.Value - 100                            'reduce the hscroll value
        End If
    End If
    If KeyCode = vbKeyRight Then                                         'if right pressed
        If HScroll1.Value < 10000 Then                                       'prevent the value to be to o big
            HScroll1.Value = HScroll1.Value + 100                           'enlarge the hscroll value
        End If
    End If
       
End Sub
-------------------------------------------------------------------------------------------------------

After that you should adjust the values in that code to fit your needs.


0
 
dbrckoviCommented:
Maybe I misunderstood your question.
Maybe you were refering to mouse buttons.

if this is the case then use this

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   
    If Button = 1 Then
        If HScroll1.Value > 99 Then
            HScroll1.Value = HScroll1.Value - 100
        End If
    End If
    If Button = 2 Then
        If HScroll1.Value < 10000 Then
            HScroll1.Value = HScroll1.Value + 100
        End If
    End If

End Sub

Note that mouse doesnt have the repeat function when holding a button. so this code wil execute only once when clicked.
You can make it repeat by using timer(s). I'll try to do it also.
0
 
dbrckoviCommented:
This is the version with timers.

Create two timers, set their interval to 10 (or try what value gives the best bahaviour) and set their enabled property to false.   One timer is used to scroll left, and other to scroll right

Assign this code to timers and Forms mousedown and mouseup events accordingly.

Theory:
When mouse button is pressed, program checks which button is pressed, and according to the button number, activates the timer. Then the timer executes its code until the button is released.
When the button is released, program checks which it was, and deactivates the timer which was activated by that number

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then Timer1.Enabled = True
    If Button = 2 Then Timer2.Enabled = True
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then Timer1.Enabled = False
    If Button = 2 Then Timer2.Enabled = False
End Sub

Private Sub Timer1_Timer()                                     'scroll left
        If HScroll1.Value > 99 Then
            HScroll1.Value = HScroll1.Value - 100
        End If
End Sub

Private Sub Timer2_Timer()                                     'scroll right
        If HScroll1.Value < 10000 Then
            HScroll1.Value = HScroll1.Value + 100
        End If
End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dbrckoviCommented:
Above code works but is still buggy.   When you press both buttons at the same time, picture shakes left and right.

Use this code to repair this:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        If Timer2.Enabled = True Then Timer2.Enabled = False: Exit Sub      'if another timer is already active then it means that another button is already pressed, and therefore deactivate it
        Timer1.Enabled = True
    End If
   
    If Button = 2 Then
        If Timer1.Enabled = True Then Timer1.Enabled = False: Exit Sub
        Timer2.Enabled = True
    End If
End Sub


Sorry that I used multiple comments to work throug this problem. It's becouse ideas came to me when I already posted a comment.
0
 
sewooAuthor Commented:
Thanks for the ideas, I'll give it a try tomorrow, but what I mean was the application has a command button called left and one called right, so I wanted to make it so that if I click on the button called "left" the screen scrolls left and if I click on "right" the screen scrolls right.
0
 
PashaModCommented:
PAQed, with points refunded (250)

PashaMod
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now