How to Create Your Own Slider Bar in Visual Basic 3.0

Last reviewed: July 20, 1995
Article ID: Q113948
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0

SUMMARY

This article describes how to make your own Slide control similar to the Windows Media Player's slide bar in Windows Accessories group or the Scroll bar in Visual Basic.

MORE INFORMATION

Visual Basic for Windows supplies Horizontal and Vertical Scroll bars, but it does not offer the ability to customize the appearance of these Scroll bars. The example code in this article uses Picture and Image controls to create a Slide bar which can be customized to include Bitmaps, background colors, and more.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a timer (Timer1) and a picture control (Picture1) to Form1.

  3. Select Picture1 by clicking it once. Now select the image control from the Toolbox and draw it onto Picture1. This new image control (Image1) should be contained by Picture1. You should not be able to drag Image1 off Picture1.

  4. Add the following code to the appropriate event procedures in Form1:

       ' Add the following to the general declarations section:
       Dim Mouse_Button As Integer
       Dim Mouse_X As Single
       Dim PicFrame As Integer
    
       Sub Form_Load ()
          Timer1.Enabled = False
          Timer1.Interval = 10
          Image1.Picture = LoadPicture("C:\VB\ICONS\ELEMENTS\EARTH.ICO")
          PicFrame = 2 * Screen.TwipsPerPixelY
          Image1.Top = 0
          Image1.Left = 0
          Picture1.Height = Image1.Height + PicFrame
       End Sub
    
       ' Enter the following two lines as one, single line of code:
       Sub Image1_MouseDown (Button As Integer, Shift As Integer, X As Single,
         Y As Single)
         Timer1.Enabled = True
         Mouse_Button = Button
         Mouse_X = Image1.Left + X
       End Sub
    
       ' Enter the following two lines as one, single line of code:
       Sub Image1_MouseMove (Button As Integer, Shift As Integer, X As Single,
          Y As Single)
          Mouse_Button = Button
          Mouse_X = Image1.Left + X
       End Sub
    
       ' Enter the following two lines as one, single line of code:
       Sub Image1_MouseUp (Button As Integer, Shift As Integer, X As Single,
          Y As Single)
          Timer1.Enabled = False
          Mouse_Button = Button
          Mouse_X = Image1.Left + X
       End Sub
    
       Sub Timer1_Timer ()
          Dim TempLeft As Integer
          If Mouse_Button = 1 Then
             TempLeft = Image1.Left
             TempLeft = Mouse_X - TempWidth / 2
             If TempLeft + Image1.Width > Picture1.Width Then
                TempLeft = Picture1.Width - Image1.Width - PicFrame
             End If
             If TempLeft < 0 Then TempLeft = 0
          End If
          If Image1.Left <> TempLeft Then Image1.Left = TempLeft
       End Sub
    
    

  5. From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program.

Select the Earth icon with your Mouse and hold the mouse button down while you move the mouse. The Earth icon will move left and right within the Picture control as you move your mouse.


Additional reference words: 3.00 slider
KBCategory: kbprg kbcode
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 20, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.