How to Use Scrollbar With Listbox in Visual Basic 6.0

Save

The ListBox control used in Visual Basic does not include Scrollbars as applicable properties as do other controls. When the lines of text within a ListBox exceed the height of the ListBox, a vertical Scrollbar is automatically added. Horizontal Scrollbars appear when the lines are longer than the width of the ListBox in a special type of ListBox that includes columns. The vertical Scrollbar adds some display functionality when the program includes the proper associated coding.

  • Open the Visual Basic 6.0 software and click on “File” and then “New Project. Use the “Standard EXE” template for this example. Save the project by clicking “File” followed by “Save Project As” and save the Form as “ScrollbarUse” and the Project as “Scroll Bar Use.”

  • Add two ListBox controls to the form by double-clicking twice on this control in the Toolbox. Arrange the two boxes so they are side by side. Click on the first ListBox labeled List1 to show the Properties on the right side of the screen. Double-click on the “List” property and replace the default setting of “(List)” with “Number.” Change the Height properties of both List1 and List2 to “1230” and the Width properties of both to 1215. Double-click on the CommandButton control in the Toolbox to add it to the form. Change the Caption property of the button to “Exit.”

  • Synchronize both Listbox controls so when the user scrolls down in one, the corresponding row in the other follows. Do this by typing the code listed below. Note: The numbers are chosen to fill both ListBox controls quickly with more lines than the size chosen in Step 2 can hold. Otherwise the Scrollbars will not appear. The “^” symbol calculates the cube of each number.

    Private Sub Form_Load()
    Dim i, n, x As Integer
    For i = 1 To 15
    List1.AddItem Choose(i, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15")
    Next
    For n = 1 To 15
    List2.AddItem Choose(n, 1 ^ 3, 2 ^ 3, 3 ^ 3, 4 ^ 3, 5 ^ 3, 6 ^ 3, 7 ^ 3, 8 ^ 3, 9 ^ 3, 10 ^ 3, 11 ^ 3, 12 ^ 3, 13 ^ 3, 14 ^ 3, 15 ^ 3)
    Next
    End Sub
    Private Sub List1_click()
    List1.TopIndex = List2.TopIndex
    List1.ListIndex = List2.ListIndex
    End Sub
    Private Sub List2_click()
    List2.TopIndex = List1.TopIndex
    List2.ListIndex = List1.ListIndex
    End Sub
    Private Sub List1_Scroll()
    List2.TopIndex = List1.TopIndex
    End Sub
    Private Sub List2_scroll()
    List1.TopIndex = List2.TopIndex
    End Sub
    Private Sub Command1_Click()
    End
    End Sub

  • Press “F5” to run this program and note when you move the Scrollbar in the first list down, the matching cube result scrolls as well. You can demonstrate a useful trick with Scrollbars in a ListBox by closing the program with the “Exit” control and reverting back to the form view in Visual Basic. Click on the “Cube” ListBox and drag it over so it covers part of the right side of the “Number” ListBox. Press “F5” to run the program again. Now it appears one control governs the actions of both boxes.

  • Change the Columns property of the Number ListBox from “0” to “1.” Replace the last number in line 4 of Step 3 to read “this is a very long sentence to enable the horizontal scroll bar.” Press “F5” again to see the effect with a horizontal Scrollbar.

Tips & Warnings

  • By adding Mouse control code, you can make the Scrollbars move in synchrony when the user clicks on another item in one list.
  • Unlike the regular Visual Basic Scrollbar properties (Vertical and Horizontal) in other controls like a TextBox, these do not exist in the ListBox control. The only way you will see Scrollbars is if the contents of the ListBox exceed the set height and/or width.

References

  • Photo Credit Jupiterimages/Pixland/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!