Results 1 to 12 of 12

Thread: Scrolling a Form

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    4

    Talking

    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

  2. #2
    Guest
    Code:
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    4

    Scrolling the form

    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!

  4. #4
    Guest
    Sure.

    Add to a Module.
    Code:
    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
    Code:
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    4

    Question

    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.

  6. #6
    Guest
    The background doesn't move because this method uses a loop to loop through each control and move them.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    4
    I see... well than is there a way to actually scroll the form and not just all the objects on the form? Thanks.

  8. #8
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    You could use a picturebox as parent of all controls and move this box, would scroll all controls and the background picture

  9. #9
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Meagatron, your code only accounts for if there is a line on the form right? It doesn't scroll a command button will it?
    Matt

  10. #10
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    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!
    Sanity is a full time job

    Puh das war harter Stoff!

  11. #11
    Guest
    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.

  12. #12
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    Can't we use the API ScrollWindowEx?
    I'm not sure how to use it...

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