Excel VBA - Navigating a Very Large Userform

Solved
TonyLucky -  
 TonyLucky -
Hello everyone,

I am working on a car racing game. I have "drawn" a track on a USF that is larger than my screen.
What I want to achieve is to make my USF "move" according to the car's position, so that it is always visible. I have added scrollbars, and I can generate two screen movements. Then, nothing happens.
Height of the USF: 1500. Width: 2000
Here's the code I'm using to position myself in the USF, based on the car's position (defined by the variable i, from the label that serves as the road case - yes, I know, a select case would be better in this case, and that's what I'll do once I resolve the current issue with your help!):

If i = 5 Then Me.ScrollTop = 0 Me.ScrollLeft = 2000 ElseIf i = 9 Then Me.ScrollTop = 2000 Me.ScrollLeft = 2000 ElseIf i = 12 Then ScrollLeft = 5000 Me.ScrollTop = 2000 ElseIf i = 15 Then ScrollLeft = 5000 Me.ScrollTop = 5000 End If


I have tried with different left and height values, without success. As far as I understand, the x and y values are the positions relative to the screen, which might explain why nothing happens beyond those values. So, how can I do it?
I admit I haven't found anything on the web.
I'm attaching my file; it's currently very simple and lightweight.
https://www.cjoint.com/c/KFzjl6qoola


PS: I am on 64-bit

Thank you in advance for your suggestions
Jean

Configuration: Windows / Firefox 89.0

5 answers

cs_Le Pivert Posted messages 8437 Status Contributor 730
 
Hello,

see this:

https://www.commentcamarche.net/faq/48339-deplacer-avec-la-souris-un-userform-sans-barre-de-fenetre

--
@+ The Woodpecker
1
TonyLucky
 
Hello,
Thank you for taking the time to read me.
I tested the code you proposed on my USF. It works with the mouse, but incompletely, even with the given example, since you cannot "exit" the screen from the top.
Furthermore, the movement of the USF needs to happen automatically, based on the position of the car.
I will try to adapt this code to automate it, but I am not sure this approach is the right one.
For your information, I have other apps with very tall USFs where I can scroll up and down without any problems using the mouse or the scroll bar. I assume that this is possible.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   Ambassadeur 1 588
 
Hello,
You didn't try with good left and top values. Try with much smaller values.
Have you displayed, at each step, the values of Me.ScrollTop & Me.ScrollLeft, for example using msgbox?
That will enlighten you.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
```html
Private Sub CommandButton1_Click() 'start the car Debug.Print "scroll " & Me.ScrollHeight & " " & Me.ScrollWidth Debug.Print "Me " & Me.Height & " " & Me.Width; "" If Application.Width < Me.Width Then 'Test if the horizontal window < the form Me.ScrollWidth = Me.Width 'Size of the horizontal scrollbar Me.Width = Application.Width End If If Application.Height < Me.Height Then 'Test if the vertical window < the form Me.ScrollHeight = Me.Height 'Size of the horizontal scrollbar Me.Height = Application.Height End If Debug.Print "scroll " & Me.ScrollHeight & " " & Me.ScrollWidth Debug.Print "Me " & Me.Height & " " & Me.Width Dim oldtop As Single, oldleft As Single Me.ScrollLeft = 0 Me.ScrollTop = 0 Do oldtop = Me.ScrollTop Me.ScrollTop = Me.ScrollTop + 1 oldleft = Me.ScrollLeft Me.ScrollLeft = Me.ScrollLeft + 1 DoEvents Loop Until oldtop = Me.ScrollTop And oldleft = Me.ScrollLeft Debug.Print oldtop Debug.Print oldleft End Sub
```
0
TonyLucky
 
Thank you for this response and this lead. I have tried many different values, large or small, but I am still stuck at the same point. In fact, it seems that the form does not go beyond a certain value, same for the movement to the right. The maximum position indicated by the msgbox is 216, for the top, as for the side.
I have tried with all the positions of the userform (manual, center, ...).
I wonder if the problem might come from the declaration of the scrollbars of the form, yet it looks like what I have done in other applications, except that here, I have both bars (horizontal and vertical).
Me.Height = 1300: Me.Width = 2000 Me.ScrollHeight = Me.Height + 100: Me.ScrollWidth = Me.Width + 100

Any idea?
0
TonyLucky
 
Hello,
In fact, as always, we need to look for simplicity... especially in computing!
I found the solution by doing away with the scrollbars and simply positioning the USF with me.top and me.left. It works perfectly.
Thank you both for your suggestions; they helped me along my way.
Have a good weekend.
0
yg_be Posted messages 23437 Registration date   Status Contributor Last intervention   1 588
 
Can you then mark the discussion as resolved?
0
TonyLucky > yg_be Posted messages 23437 Registration date   Status Contributor Last intervention  
 
As a free visitor, I do not have access to the resolved function.
Without wanting to impose, could you kindly do it if you can?
Thank you and have a great weekend.
0