PDA

Click to See Complete Forum and Search --> : Scrolling a Form


Bowser0044
Nov 26th, 2000, 02:22 PM
How can I get a form to scroll like a text box does? For example, the focus of the form is 0,0 to 500,500. How would I make it 100,100 to 600,600? Thanks

Nov 26th, 2000, 04:31 PM
Dim VPos As Integer
Dim HPos As Integer

Private Sub Form_Load()
'Change the following numbers to the Full height and width of your Form
intFullHeight = 8000
intFullWidth = 8000
'This is the how much of your Form is displayed
intDisplayHeight = Me.Height
intDisplayWidth = Me.Width

With VScroll1
.Height = Me.ScaleHeight
.Min = 0
.Max = intFullHeight - intDisplayHeight
.SmallChange = Screen.TwipsPerPixelX * 10
.LargeChange = .SmallChange
End With
With HScroll1
.Width = Me.ScaleWidth
.Min = 0
.Max = intFullWidth - intDisplayWidth
.SmallChange = Screen.TwipsPerPixelX * 10
.LargeChange = .SmallChange
End With
End Sub

Sub ScrollForm(Direction As Byte)
Dim CTL As Control

'Scroll Vertically
If Direction = 0 Then
For Each CTL In Me.Controls
'Make sure it's not a ScrollBar
If Not (TypeOf CTL Is VScrollBar) And Not (TypeOf CTL Is HScrollBar) Then
'If it's a Line then
If TypeOf CTL Is Line Then
CTL.Y1 = CTL.Y1 + VPos - VScroll1.Value
CTL.Y2 = CTL.Y2 + VPos - VScroll1.Value
Else
CTL.Top = CTL.Top + VPos - VScroll1.Value
End If
End If
Next
VPos = VScroll1.Value
Else
'Scroll Horizontally
For Each CTL In Me.Controls
'Make sure it's not a ScrollBar
If Not (TypeOf CTL Is VScrollBar) And Not (TypeOf CTL Is HScrollBar) Then
'If it's a Line then
If TypeOf CTL Is Line Then
CTL.X1 = CTL.X1 + HPos - HScroll1.Value
CTL.X2 = CTL.X2 + HPos - HScroll1.Value
Else
CTL.Left = CTL.Left + HPos - HScroll1.Value
End If
End If
Next
HPos = HScroll1.Value
End If
End Sub

Private Sub HScroll1_Change()
ScrollForm 1
End Sub

Private Sub HScroll1_Scroll()
ScrollForm 1
End Sub

Private Sub VScroll1_Change()
ScrollForm 0
End Sub

Private Sub VScroll1_Scroll()
ScrollForm 0
End Sub

Bowser0044
Nov 26th, 2000, 05:10 PM
Wow! This is JUST what I needed! However, could you do one tiny more favor for me? I really do not understand how this code works, making it very difficult for me to use. Would it be possible to set it up in a code module with two sub-rounties vmove and hmove? That would be incredible if you could, if you cannot thanks aways!

Nov 26th, 2000, 05:31 PM
Sure.

Add to a Module.

Public VPos As Integer
Public HPos As Integer
Public intDisplayWidth As Integer
Public intDisplayHeight As Integer
Public intFullWidth As Integer
Public intFullHeight As Integer

Private Sub ScrollForm(Direction As Byte, frm As Form)
Dim CTL As Control

'Scroll Vertically
If Direction = 0 Then
For Each CTL In frm.Controls
'Make sure it's not a ScrollBar
If Not (TypeOf CTL Is VScrollBar) And Not (TypeOf CTL Is HScrollBar) Then
'If it's a Line then
If TypeOf CTL Is Line Then
CTL.Y1 = CTL.Y1 + VPos - frm.VScroll1.Value
CTL.Y2 = CTL.Y2 + VPos - frm.VScroll1.Value
Else
CTL.Top = CTL.Top + VPos - frm.VScroll1.Value
End If
End If
Next
VPos = frm.VScroll1.Value
Else
'Scroll Horizontally
For Each CTL In frm.Controls
'Make sure it's not a ScrollBar
If Not (TypeOf CTL Is VScrollBar) And Not (TypeOf CTL Is HScrollBar) Then
'If it's a Line then
If TypeOf CTL Is Line Then
CTL.X1 = CTL.X1 + HPos - frm.HScroll1.Value
CTL.X2 = CTL.X2 + HPos - frm.HScroll1.Value
Else
CTL.Left = CTL.Left + HPos - frm.HScroll1.Value
End If
End If
Next
HPos = frm.HScroll1.Value
End If
End Sub

Sub hMove(frm As Form)
ScrollForm 1, frm
End Sub

Sub VMove(frm As Form)
ScrollForm 0, frm
End Sub


Add to a Form with HScroll1 and VScroll1

Private Sub Form_Load()
'Change the following numbers to the Full height and width of your Form
intFullHeight = 8000
intFullWidth = 10000
'This is the how much of your Form is displayed
intDisplayHeight = Me.Height
intDisplayWidth = Me.Width

With VScroll1
.Height = Me.ScaleHeight
.Min = 0
.Max = intFullHeight - intDisplayHeight
.SmallChange = Screen.TwipsPerPixelX * 10
.LargeChange = .SmallChange
End With
With HScroll1
.Width = Me.ScaleWidth
.Min = 0
.Max = intFullWidth - intDisplayWidth
.SmallChange = Screen.TwipsPerPixelX * 10
.LargeChange = .SmallChange
End With
End Sub



Private Sub HScroll1_Change()
hMove Me 'Move horizontally
End Sub

Private Sub HScroll1_Scroll()
hMove Me 'Move horizontally
End Sub

Private Sub VScroll1_Change()
VMove Me 'Move vertically
End Sub

Private Sub VScroll1_Scroll()
VMove Me 'Move vertically
End Sub


This uses hMove to move horizontally and VMove to move vertically.

Bowser0044
Nov 26th, 2000, 05:36 PM
Sorry to both you once again, but I have another question. When I use this, it does this it does not scroll the background on the form. Also, how can I go backwards? When I click up on the vscroll bar, it goes the same way as if I clicked down. Same with the hscroll bar. Thanks again.

Nov 26th, 2000, 05:56 PM
The background doesn't move because this method uses a loop to loop through each control and move them.

Bowser0044
Nov 26th, 2000, 06:01 PM
I see... well than is there a way to actually scroll the form and not just all the objects on the form? Thanks.

Fox
Nov 27th, 2000, 05:25 AM
You could use a picturebox as parent of all controls and move this box, would scroll all controls and the background picture :)

MPrestonf12
Nov 27th, 2000, 03:46 PM
Meagatron, your code only accounts for if there is a line on the form right? It doesn't scroll a command button will it?

/\/\isanThr0p
Nov 28th, 2000, 09:36 AM
Hey Do you see the else after the if asking if it's a line?
That makes other controls like Commandbuttons move, too.

Nice code Megatron!

Nov 28th, 2000, 02:52 PM
Originally posted by MPrestonf12
Meagatron, your code only accounts for if there is a line on the form right? It doesn't scroll a command button will it?

/\/\isanThr0p is correct. It will scroll regardless of what control is on there. Read the comments and it will direct you.

Mad Compie
Dec 3rd, 2000, 04:32 AM
Can't we use the API ScrollWindowEx?
I'm not sure how to use it...