CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004
    Posts
    10

    Incorporating scrollbars for a form

    Hello all!!
    Can anyone tell me how can i add horizontal and vertical scrollbars to a form?I dont see any "scrollbar" in the properties toolbar for a form,or am i missing something?....

    Thanks!!

  2. #2
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: Incorporating scrollbars for a form

    It doesn't work quite like that.

    There are two ways to do this, one is to put everything into a picture box, and have your scroll bars outside of the picture box. When the user moves the scroll bar, you move the picture box.

    The other way is to use an API, which I think Wizbang may have replied to some time ago - I know someone posted how to move the screen around using an API. If you search the forum, perhaps you'll get more information.

    If you are using the picture box method, you will need to determine whether or not to display the scroll bars - this is done in the form resize event. EG:
    Code:
    Private Sub Form_Resize()
        
        Const hScrollbarPos = 600
        Const vScrollbarPos = 350
        
    '
    ' If the window is minimized, there is no point
    ' in displaying scroll bars
    '
        If Me.WindowState = vbMinimized Then
            Exit Sub
        End If
        
        If Me.Width > picScreen.Width Or Me.Width = picScreen.Width Then
            picScreen.Left = 0
        End If
        If Me.Height > picScreen.Height Or Me.Height = picScreen.Height Then
            picScreen.Top = 0
        End If
        
        If Me.Width < picScreen.Width Then
            hsbScrollBar.Visible = True
            hsbScrollBar.Width = Me.Width - (vsbScrollBar.Width + 200)
            hsbScrollBar.Max = (picScreen.Width - Me.Width) + vsbScrollBar.Width
            hsbScrollBar.Top = Me.Height - hScrollbarPos
            hsbScrollBar.Enabled = True
        Else
            hsbScrollBar.Enabled = False
            hsbScrollBar.Visible = False
        End If
    
        If Me.Height < picScreen.Height Then
            vsbScrollBar.Left = Me.Width - vScrollbarPos
            vsbScrollBar.Max = (picScreen.Height - Me.Height) + hsbScrollBar.Height
            'vsbScrollBar.Height = Me.Height - (hsbScrollBar.Height + 100)
            vsbScrollBar.Height = Me.Height - (hsbScrollBar.Height + 200)
            vsbScrollBar.Visible = True
            vsbScrollBar.Enabled = True
            hsbScrollBar.Top = Me.Height - hScrollbarPos
        Else
            vsbScrollBar.Enabled = False
            vsbScrollBar.Visible = False
        End If
        
    End Sub
    picScreen (above) is the name of my picture box containing all the controls.

    Scroll bar events as follows:
    Code:
    Private Sub hsbScrollBar_Change()
    
        Dim nPos As Integer
        
        nPos = Me.Width - hsbScrollBar
        nPos = Me.Width - nPos
        
        picScreen.Left = nPos * -1
        
    End Sub
    
    Private Sub vsbScrollBar_Change()
    
        Dim nPos As Integer
        
        nPos = Me.Height - vsbScrollBar
        nPos = Me.Height - nPos
    
        picScreen.Top = nPos * -1
        
    End Sub
    HTH
    JP

    Please remember to rate all postings.

  3. #3
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Arrow Re: Incorporating scrollbars for a form

    Another way (a trick in fact) is to host your main form inside a MDI Form. and make the size of your form Larger than the MDI form.
    This way you can benifit from the automatic scrollbar functionality that exists in MDI forms.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

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

Featured