September 5, 1995
This article explains how to scroll text horizontally within a Picture Box control in Microsoft® Visual Basic®.
You can add visual effects to your Microsoft® Visual Basic® applications that add interest or grab the user's attention. You can do this, for example, by adding a scrolling message to your program to create a marquee effect.
A scrolling message is a string of text that is continually displayed in a control. To achieve this effect in your program, you can use the Timer and Picture Box controls.
The Print function lets you display a character in a Picture Box control. The character is printed at the current x- and y-coordinates within the control. Therefore, to display an entire string in the Picture Box, you first need to extract each character from the target string you want to display. Then you can use the Print function to display that character in the Picture Box control.
To make the text in the Picture Box scroll continuously, you simply keep track of your position within the target string. When you reach the end of the string, set the pointer to the beginning of the string. Using the Timer control, you can send (that is, print) a character to the Picture Box at specific time intervals. This causes the message to print continuously in the Picture Box control.
This program shows how to scroll text horizontally in a Picture Box control.
Private Sub Timer1_Timer()
ShowMessage
End Sub
Sub ShowMessage()
Static MsgPtr As Integer
Static MyText As String
If Len(MyText) = 0 Then
MsgPtr = 1
MyText = "Welcome to Visual Basic programming!"
End If
Picture1.Cls
Picture1.Print Mid$(MyText, MsgPtr); MyText;
MsgPtr = MsgPtr + 1
If MsgPtr > Len(MyText) Then
MsgPtr = 1
End If
End Sub
Run the example program by pressing f5. The "Welcome to Visual Basic programming!" message will scroll across the Picture Box control continuously until you quit the program.