scrollbar in vb

how to implement a scrollbar in vb form.
my form is mdi child type. my problem is such that i have a picture box in a mdi child form. the size of picture box changes during run time, so the need of scrollbar is there if the picture box size is greater than mdi child form. how to do this so that by scrolling i can completely see the picture
reply  if possible with a sourcecode
this scrolling you may call form level scrolling.
in vc and java it is automatically implemented but in vb it is not
Who is Participating?
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
It would have been easier with your e-mail address but...

Create a user control and place an HScrollBar control and a VScrollBar control then paste this code:

Option Explicit

' need to listen to the form's resize event
Dim WithEvents m_form As Form

' if set, then ignore scroll events from scrollbar controls
Private m_IgnoreScrollEvents As Boolean

' the amount we have scrolled in x/y direction (pixels)
Private m_ScrollX As Long
Private m_ScrollY As Long

' width / height of content area of the form (pixels)
Private m_ContentWidth As Long
Private m_ContentHeight As Long

' when the form we are on resizes, then we need to reposition our control
Private Sub m_form_Resize()
  ' move the control to take up the entire form area
  Extender.Left = 0
  Extender.Top = 0
  Extender.Width = m_form.ScaleWidth
  Extender.Height = m_form.ScaleHeight
  ' move the scrollbars on the control to right/bottom edges
  VScroll1.Left = ScaleWidth - VScroll1.Width
  VScroll1.Height = ScaleHeight - HScroll1.Height
  HScroll1.Width = ScaleWidth - VScroll1.Width
  HScroll1.Top = ScaleHeight - HScroll1.Height
  ' move the picturebox (lower right corner) into position
  Picture1.Left = VScroll1.Left
  Picture1.Top = HScroll1.Top
  ' since size has changed, then recompute scrollbar values
End Sub

Private Sub UserControl_Paint()
  ' at design time, just display the control name
  If Not Ambient.UserMode Then
    CurrentX = 0
    CurrentY = 0
    Print Ambient.DisplayName
  End If
End Sub

Private Sub UserControl_Resize()
  ' at design time, we are a fixed size control
  If Not Ambient.UserMode Then
    Size TextWidth(Ambient.DisplayName), TextHeight(Ambient.DisplayName)
  End If
End Sub

' look at the form and decide which area is occupied by controls
Private Sub CalcContentArea()
  Dim o As Object
  Dim X As Long
  Dim Y As Long
  m_ContentWidth = 0
  m_ContentHeight = 0
  ' find the controls that are furthest towards the right / bottom
  For Each o In ParentControls
    On Error GoTo SkipControl
    ' if it's on the form, but it's not me
    If o.Container Is m_form And Not o Is Extender Then
      X = ScaleX(o.Left, vbContainerPosition, vbPixels) + ScaleX(o.Width, vbContainerSize, vbPixels)
      If X > m_ContentWidth Then
        m_ContentWidth = X
      End If
      Y = ScaleY(o.Top, vbContainerPosition, vbPixels) + ScaleX(o.Height, vbContainerSize, vbPixels)
      If Y > m_ContentHeight Then
        m_ContentHeight = Y
      End If
    End If
  Next o
End Sub

' determine current scrollbar values and enabling
Private Sub CalcScrollBars()
  Dim ViewWidth As Long
  Dim ViewHeight As Long
  ' get the size of the view (form minus scrollbars)
  ViewWidth = ScaleX(m_form.ScaleWidth, vbContainerSize, vbPixels) - ScaleX(VScroll1.Width, ScaleMode, vbPixels)
  ViewHeight = ScaleY(m_form.ScaleHeight, vbContainerSize, vbPixels) - ScaleY(HScroll1.Height, ScaleMode, vbPixels)
  If ViewWidth < 0 Then ViewWidth = 0
  If ViewHeight < 0 Then ViewHeight = 0
  ' prevent rentrancy on scroll events
  m_IgnoreScrollEvents = True
  ' enable horizontal scrollbar appopriately
  If (ViewHeight < m_ContentHeight) Then
    VScroll1.Enabled = True
    VScroll1.Max = m_ContentHeight - ViewHeight
    If (m_ScrollX > VScroll1.Max) Then VScroll1.Max = m_ScrollX
    VScroll1.Value = m_ScrollX
    VScroll1.LargeChange = ViewHeight / 2
    VScroll1.SmallChange = ViewHeight / 8
    VScroll1.Enabled = False
  End If
  ' enable vertical scrollbar appopriately
  If (ViewWidth < m_ContentWidth) Then
    HScroll1.Enabled = True
    HScroll1.Max = m_ContentWidth - ViewWidth
    If (m_ScrollY > HScroll1.Max) Then HScroll1.Max = m_ScrollY
    HScroll1.Value = m_ScrollY
    HScroll1.LargeChange = ViewWidth / 2
    HScroll1.SmallChange = ViewWidth / 8
    HScroll1.Enabled = False
  End If
  m_IgnoreScrollEvents = False
End Sub

' when the control is shown in run mode, kick into action
Private Sub UserControl_Show()
  If Ambient.UserMode Then
    ' sink the events of the form, so we can watch for resizes
    Set m_form = Parent
    ' transparent, so only scrollbars are visible
    BackStyle = 0
    ' bring me to the top
    Extender.ZOrder 0
    ' calculate area of form occupied by controls
    ' fake a resize
    ' show our subcontrols (normally hidden at design time)
    VScroll1.Visible = True
    HScroll1.Visible = True
    Picture1.Visible = True
  End If
End Sub

' handle user scrolling vertically
Private Sub VScroll1_Change()
  If m_IgnoreScrollEvents Then Exit Sub
  Scroll 0, VScroll1.Value - m_ScrollY
End Sub

' handle user scrolling horizontally
Private Sub HScroll1_Change()
  If m_IgnoreScrollEvents Then Exit Sub
  Scroll HScroll1.Value - m_ScrollX, 0
End Sub

' scroll the subcontrols of the form by a delta amount
Private Sub Scroll(dx As Long, dy As Long)
  Dim o As Object
  For Each o In ParentControls
    On Error GoTo SkipControl
    ' if it's on the form, but not me
    If o.Container Is m_form And Not o Is Extender Then
      o.Left = o.Left - ScaleX(dx, vbPixels, vbContainerPosition)
      o.Top = o.Top - ScaleY(dy, vbPixels, vbContainerPosition)
    End If
  Next o
  m_ScrollX = m_ScrollX + dx
  m_ScrollY = m_ScrollY + dy
End Sub

You can now place this control on your and VOIL@... Give it a try. It definitely worths it!
I don't know whether MDI child forms have this feature. But you can do one thing
When the picture box changes it size, you can trap the RESIZE event of it and explicitly resize your form.

By this way you can always have your child form showing the picture box. In case the child form grows bigger, then the MDI Form will automatically get scrollbars as it "scrollbar" property is true by default.

My 2c worth  :-)

You place a frame on your form and then place the picture box on that. Even though the picture box's size changes, it will be placed in the frame, so, the size of the frame does not change. Now, you change the scroll bars' properties like min, max dynamically. Then when user scrolls, in the scroll and change events, change the position i.e, left, top properties of the picture box. This should work. Any questions, please feel free to contact.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Éric MoreauSenior .Net ConsultantCommented:
I can send you a sample project that does not uses a picture box but 2 scroll bars instead.

Give me your e-mail address if interested.
modijsAuthor Commented:
i want it as a type of perfect window application using  frame is not an appropriate.  i have to make an application in which i have to draw plan.  it is just like auto cad application.  so in this my area changes dyanmically so what should be done to draw this on the client area of form where the size can be greater than form.  i have used picture box in the form for drawing.  but while changing size of picture box as the needed or size of picture box should be increased when zoom is implemented it does not come within the form client area, scroll bars can help to scroll the area.
Forms other than MDI Forms cannot by default have scroll bars.
For any other forms you will have to prgram it putting a vertical scroll bar on ur form.
Preferably put the picture box in a frame control. then in the change event of the vertical scroll bar manipulate the top of the frame(if u dont want to use the frame u will have to manipulate the tops of all the controls on the form other than scroll bar) to give a scrolling effect.

Éric MoreauSenior .Net ConsultantCommented:
I just found source code for an ActiveX control that implements automatic scroll bars on forms.

I can send it to you be e-mail by I need your address!
modijsAuthor Commented:
i have found that scrollbar can be implemented using api's function like showscrollbar etc. send me the source code for implementing using this.
modijsAuthor Commented:
thanks for ur support.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.