Results 1 to 7 of 7

Thread: Looking for a better Scroll bar code (badly needed)

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Resolved Looking for a better Scroll bar code (badly needed)

    The code I'm using now has worked the best, compared to other codes, for all my apps until now.
    The problem is that I have a TON of Picture Boxes and Labels that are loaded at runtime and they go somewhat beyond the maximum stretch of the scrollbars which effectively makes me unable to scroll over enough and view those Picture Boxes and Labels.

    SBar - A Picture Box on Form1 with .Align = 2 - Align Bottom
    HS - A HScrollBar
    VS - A VScrollBar

    Code as follows:
    VB Code:
    1. Dim VSLVal As Integer
    2. Dim HSLVal As Integer
    3. Dim HMax As Integer
    4. Dim VMax As Integer
    5.  
    6. Private Sub Form_Load()
    7. On Error Resume Next
    8. Dim Cntrl As Control
    9.  
    10. For Each Cntrl In Me.Controls
    11.  If (Not TypeOf Cntrl Is HScrollBar) And _
    12.     (Not TypeOf Cntrl Is VScrollBar) Then
    13.   If (Cntrl.Top + Cntrl.Height + 125) > VMax Then
    14.    VMax = Cntrl.Top + Cntrl.Height + 125
    15.   End If
    16.   If (Cntrl.Left + Cntrl.Width + 125) > HMax Then
    17.    HMax = Cntrl.Left + Cntrl.Width + 125
    18.   End If
    19.  End If
    20. Next
    21. On Error GoTo 0
    22.  
    23. With HS
    24. .Left = 0
    25. .Top = Me.ScaleHeight - .Height
    26. SBar.Height = .Height
    27. .Width = Me.ScaleWidth - VS.Width
    28. .Max = HMax - .Width
    29. If .Max < 0 Then .Max = 0
    30. End With
    31.  
    32. With VS
    33. .Height = Me.ScaleHeight - HS.Height
    34. .Left = Me.ScaleWidth - .Width
    35. .Top = 0
    36. .Max = VMax - .Height
    37. If .Max < 0 Then .Max = 0
    38. End With
    39.  
    40. If HS.Max <= 0 Then HS.Enabled = False
    41. If VS.Max <= 0 Then VS.Enabled = False
    42. End Sub
    43.  
    44. Private Sub Form_Resize() 'complete
    45. If Me.WindowState = 1 Then
    46.  Exit Sub
    47. ElseIf Me.Height < 2525 Then
    48.  Me.Height = 2525
    49. ElseIf Me.Width < 2525 Then
    50.  Me.Width = 2525
    51. End If
    52.  
    53. With HS
    54. .Left = 0
    55. .Top = Me.ScaleHeight - .Height
    56. .Width = Me.ScaleWidth - VS.Width
    57. .Max = HMax - .Width
    58. End With
    59.  
    60. With VS
    61. .Height = Me.ScaleHeight - HS.Height
    62. .Left = Me.ScaleWidth - .Width
    63. .Top = 0
    64. .Max = VMax - .Height
    65. End With
    66.  
    67. If HS.Max <= 0 Then
    68.  HS.Enabled = False
    69.  HS.Max = 0
    70. Else
    71.  HS.Enabled = True
    72. End If
    73. If VS.Max <= 0 Then
    74.  VS.Enabled = False
    75.  VS.Max = 0
    76. Else
    77.  VS.Enabled = True
    78. End If
    79. End Sub
    80.  
    81. Private Sub HSPositionChanged() 'complete
    82. On Error Resume Next
    83. Dim Val As Integer, Cntrl As Control
    84. Val = HSLVal - HS.Value
    85. For Each Cntrl In Me.Controls
    86.  If TypeOf Cntrl Is HScrollBar Or _
    87.     TypeOf Cntrl Is VScrollBar Or _
    88.     TypeOf Cntrl Is Menu Then
    89.  ElseIf TypeOf Cntrl Is Line Then
    90.   Cntrl.X1 = Cntrl.X1 + Val
    91.   Cntrl.X2 = Cntrl.X2 + Val
    92.  Else
    93.   Cntrl.Left = Cntrl.Left + Val
    94.  End If
    95. Next
    96. HSLVal = HS.Value
    97. End Sub
    98.  
    99.  
    100. Private Sub VSPositionChanged() 'complete
    101. On Error Resume Next
    102. Dim Val As Integer, Cntrl As Control
    103. Val = VSLVal - VS.Value
    104. For Each Cntrl In Me.Controls
    105.  If TypeOf Cntrl Is HScrollBar Or _
    106.     TypeOf Cntrl Is VScrollBar Or _
    107.     TypeOf Cntrl Is Menu Then
    108.  ElseIf TypeOf Cntrl Is Line Then
    109.   Cntrl.Y1 = Cntrl.Y1 + Val
    110.   Cntrl.Y2 = Cntrl.Y2 + Val
    111.  Else
    112.   Cntrl.Top = Cntrl.Top + Val
    113.  End If
    114. Next
    115. VSLVal = VS.Value
    116. End Sub
    117.  
    118. Public Sub resizescrolbars()
    119. 'VS.Value = 0
    120. 'HS.Value = 0
    121. On Error Resume Next
    122. Dim Cntrl As Control
    123. For Each Cntrl In Me.Controls
    124.  If (Not TypeOf Cntrl Is HScrollBar) And _
    125.     (Not TypeOf Cntrl Is VScrollBar) Then
    126.   If (Cntrl.Top + Cntrl.Height + 125) > VMax Then
    127.    VMax = Cntrl.Top + Cntrl.Height + 125
    128.   End If
    129.   If (Cntrl.Left + Cntrl.Width + 125) > HMax Then
    130.    HMax = Cntrl.Left + Cntrl.Width + 125
    131.   End If
    132.  End If
    133. Next
    134. On Error GoTo 0
    135. With HS
    136. .Left = 0
    137. .Top = Me.ScaleHeight - .Height
    138. SBar.Height = .Height
    139. .Width = Me.ScaleWidth - VS.Width
    140. .Max = HMax - .Width
    141. If .Max < 0 Then .Max = 0
    142. End With
    143. With VS
    144. .Height = Me.ScaleHeight - HS.Height
    145. .Left = Me.ScaleWidth - .Width
    146. .Top = 0
    147. .Max = VMax - .Height
    148. If .Max < 0 Then .Max = 0
    149. End With
    150. If HS.Max <= 0 Then HS.Enabled = False
    151. If VS.Max <= 0 Then VS.Enabled = False
    152. If Me.WindowState = 1 Then
    153.  Exit Sub
    154. ElseIf Me.Height < 2525 Then
    155.  Me.Height = 2525
    156. ElseIf Me.Width < 2525 Then
    157.  Me.Width = 2525
    158. End If
    159. With HS
    160. .Left = 0
    161. .Top = Me.ScaleHeight - .Height
    162. .Width = Me.ScaleWidth - VS.Width
    163. .Max = HMax - .Width
    164. End With
    165. With VS
    166. .Height = Me.ScaleHeight - HS.Height
    167. .Left = Me.ScaleWidth - .Width
    168. .Top = 0
    169. .Max = VMax - .Height
    170. End With
    171. If HS.Max <= 0 Then
    172.  HS.Enabled = False
    173.  HS.Max = 0
    174. Else
    175.  HS.Enabled = True
    176. End If
    177. If VS.Max <= 0 Then
    178.  VS.Enabled = False
    179.  VS.Max = 0
    180. Else
    181.  VS.Enabled = True
    182. End If
    183. End Sub
    184.  
    185. Private Sub HS_Change() 'complete
    186. HSPositionChanged
    187. End Sub
    188. Private Sub HS_Scroll() 'complete
    189. HSPositionChanged
    190. End Sub
    191.  
    192. Private Sub vs_Change() 'complete
    193. VSPositionChanged
    194. End Sub
    195. Private Sub vs_Scroll() 'complete
    196. VSPositionChanged
    197. End Sub

    And in a Module I have:
    VB Code:
    1. Public Sub drawstuff(Index As Integer)
    2. Call UnloadData 'unloads all the controls so that when we load I have the Right number of controls
    3. Form1.HS.Value = 0
    4. Form1.VS.Value = 0
    5. Call LoadData(Index) 'loads the controls
    6. Call Form1.EnableSelection 'stuff for moving things with mouse
    7. 'resize the scrollbars
    8. Call Form1.resizescrolbars
    9. Form1.HS.Value = 0
    10. Form1.VS.Value = 0
    11. End Sub

    Here are all of the Picture box’s Tool tip, Top, and Left as given by debug.print:
    (see next post, this one is too long)



    PS:I can post the whole 6 mb project if needed, (about 150k is the actual program, the rest of it are graphics).
    Last edited by Tontow; Feb 4th, 2007 at 01:36 AM.

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