Solved

Scrolling help

Posted on 2003-10-28
8
159 Views
Last Modified: 2010-05-03
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
Comment
Question by:sewoo
  • 4
8 Comments
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9639916
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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9639935
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
 
LVL 11

Expert Comment

by:dbrckovi
ID: 9639964
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:dbrckovi
ID: 9640016
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
 

Author Comment

by:sewoo
ID: 9640052
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
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 10390451
PAQed, with points refunded (250)

PashaMod
Community Support Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question