Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Scroll bar

Posted on 2003-02-26
Medium Priority
Last Modified: 2008-02-26
Using VB6, could someone please help me with the basic code for a scroll bar. I have a label with a list of information within it. "I like the way it works for my project". Any ways, I've never messed around with scroll bars, so how do I code the bar? My label has information that's more than the label and I just want to be able to scroll down the label.

Question by:vharrig
  • 2
  • 2
  • 2
  • +1
LVL 14

Accepted Solution

aelatik earned 200 total points
ID: 8027403
Use a textbox instead if a label, i don't think you can scroll a label. With a textbox you can set the property to multiline and scrollbars ( vertical, horizontal or both )

Author Comment

ID: 8027527

Thanks that works, have any idea's why the scroll bar won't show up until I click on the text box. Then it show's up fine and works correctly.

Expert Comment

ID: 8027542
Hello vharrig

Yes aelatik is right, just use a textbox and change the properties such as the background color, Appearance and border so that if looks the same as a label.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8027550
It will probably only show up if you have enough text inside it for the need of a scroll bar.

Author Comment

ID: 8027583

Expert Comment

ID: 8027746
Argh!  And I just finished this code to...  This gives you a scrolling label.  Try this out and see if you like it.  Create a text file called Form1.frm and paste the following code into it via NotePad or WordPad, then add it to a new VB project and run it:

Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   4230
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   6210
   LinkTopic       =   "Form1"
   ScaleHeight     =   4230
   ScaleWidth      =   6210
   StartUpPosition =   3  'Windows Default
   Begin VB.VScrollBar vScroll
      Height          =   975
      Left            =   3120
      TabIndex        =   1
      Top             =   360
      Width           =   255
   Begin VB.Label lblText
      Caption         =   "lblText"
      Height          =   855
      Left            =   120
      TabIndex        =   0
      Top             =   480
      Width           =   2895
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Lines() As String
Private LineCount As String
Private LinesToShow As Integer

Private Sub SplitText(OrigText As String)

    Dim CurrPos As Long
    Dim PreviousPos As Long
    Dim Text As String
    Dim NewLine As String
    ' Init
    LineCount = 0
    CurrPos = 0
    Text = OrigText
    If Text = "" Then Exit Sub
    ' Loop until we have analyzed the entire string
        ' Store this position
        PreviousPos = CurrPos
        CurrPos = CurrPos + 1
        NewLine = ""
        ' Move to the next space in the string
        ' NOTE: You may want to modify this to look for any type of white space
        CurrPos = InStr(CurrPos, Text, " ")
        If CurrPos = 0 Then
            ' Out of text, use it all
            NewLine = Text
            ' Determine if this is more than one full line
            NewLine = Left(Text, CurrPos)
        End If
        If Me.TextWidth(NewLine) < lblText.Width Then
            If CurrPos > 0 Then
                ' Don't use this
                NewLine = ""
            End If
        ElseIf PreviousPos > 1 Then
            ' We went over, use the previous value for the line
            NewLine = Left(Text, PreviousPos - 1)
            ' Reset the current position
            CurrPos = 1
        End If
        If NewLine <> "" Then
            ' Store this line
            ReDim Preserve Lines(LineCount) As String
            Lines(LineCount) = NewLine
            LineCount = LineCount + 1
            ' Remove this text from the main string
            Text = LTrim(Mid(Text, Len(NewLine) + 1))
        End If
    Loop Until CurrPos = 0

End Sub

Private Sub Form_Load()

    Dim I As Integer
    LinesToShow = 4
    ' NOTE: You need to use the same font for the form as for the label for this to work
    Call SplitText("This is the text that is being displayed in the label.  It will be word-wrapped by the SplitText routine and split into it's individual lines so that the scroll bar can scroll the text with some logic...  I'm trying to write a whole lot here so that we will see quite a bit of word wrap and all will be good with the world.  You might want to set this up to read in a text file and display it.  This would definately require retooling the SplitText function though, since it would need to be able to properly handle [Enter] characters (chr(10) & chr(13)), but I wish you the best in your wrapping endeavors.")
    ' Set the size of the scroll bar so that the maxvalue will display the last line at the bottom
    vScroll.Max = LineCount - LinesToShow
    vScroll.Value = 0
    Call vScroll_Change

End Sub

Private Sub vScroll_Change()

    Dim I As Integer
    Dim Text As String
    For I = 0 To LinesToShow - 1
        ' Get this line from the array
        If Text <> "" Then Text = Text & vbCrLf
        Text = Text & Replace(Lines(vScroll.Value + I), "&", "&&")
    ' Display this text in the label
    lblText.Caption = Text

End Sub

Private Sub vScroll_Scroll()

    Call vScroll_Change

End Sub

Expert Comment

ID: 8027774
Oh, as a side note, you can calculate the "LinesToShow" variable using Me.TextHeight().  Also, any time that you resize the label you would need to pass the original string back through the "SplitText()" routine.

If you have any questions about this please feel free to ask as it's no longer about the points, just about helping you get the results you desire...

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question