How to Auto Scroll Text in a Textbox Using VB.NET
Creating a Visual Basic.NET application that auto scrolls text in a TextBox control can easily be accomplished using String arrays and a timer. A timer control in VB.NET will execute code continuously until the program ends. Use a String array to fill it with the text you want to scroll, and display different elements of the array in the TextBox using the timer to show the scrolling effect.
Instructions
-
-
1
Launch Microsoft Visual Studio, click "New Project" from the left pane of your computer screen and expand "Visual Basic" below "Installed Templates." Click "Windows" and double-click "Windows Forms Application" from the center of the dialog window to create a new project.
-
2
Double-click "Timer" to add one to your form. Double-click "TextBox" to add a new text box to your form. Double-click the form to create a form load event.
-
-
3
Add the following code above the form load event to create three global variables, including the one that will hold your scrolling text:
Dim scrollingText As String = "This is my scrolling text "
Dim txtStr(scrollingText.Length - 1) As String
Dim txtPos As Integer = -1
-
4
Add the following code inside the form load event to loop through the scrolling text and start the timer:
For idx As Integer = 0 To UBound(txtStr)
Dim workedString As String = ""
workedString = scrollingText.Substring(idx) & " " & scrollingText.Substring(0, idx)
txtStr(idx) = workedString
Next
Timer1.Interval = 75
Timer1.Enabled = True
Timer1.Start()
-
5
Switch back to form design view and double-click "Timer1" to create a tick event for the timer. Add the following code inside the event:
txtPos += 1
Dim timerStr As String
timerStr = txtStr(txtPos)
Label1.Text = timerStr
If txtPos = UBound(txtStr) Then txtPos = -1
-
6
Press "F5" to run the program and display scrolling text using the text box.
-
1
References
- Photo Credit Comstock/Comstock/Getty Images