DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Introduction to WinForms - Scrolling Text in .NET (VB)

This tutorial shows how we can create a simple application to scroll user-input text, marquee-style, in VB.

This tutorial is an introduction to creating and coding WinForms in Visual Studio.NET. This simple program will allow you to enter text and then on the click of a button, the text will scroll until the program is closed. After learning the basics, there are more optimized ways of coding, and multi-threading can be incorporated into this program to make it more stable and user-friendly.

First, we add two labels, a text box and a button. One of the labels is where the scroll will take place, and the other is to simply tell the user to enter text into the text box. The Form should look something like this:

Img01

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Next, we add logic to the TextChanged event of the text box. We can do this by double-clicking on the text box in design view. We will enable the button when text is entered, and disable the button on Form Load. The code will look something like this:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Trim() = "" Then
btnScroll.Enabled = False
Else
btnScroll.Enabled = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnScroll.Enabled = False
End Sub

Next, we add logic to the Click event of the button. This code will call the method that causes the text to scroll: StartScroll(). The StartScroll method first adds a space at the end of the user-inputted string, and then loops until the program closes. The loop is constantly removing the last character in the string and putting it back at the start, giving it the illusion that the text is moving. After each loop, the thread is paused for 100 milliseconds, then repeats.
The code will look something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScroll.Click
btnScroll.Enabled = False
StartScroll()
End Sub

Private Sub StartScroll()
Dim sb As New System.Text.StringBuilder(textBox1.Text & " ")
Do
Dim ch As Char = sb(sb.Length - 1)
sb.Remove(sb.Length - 1, 1)
sb.Insert(0, ch)
Label1.Text = sb.ToString()
Label1.Refresh()
System.Threading.Thread.Sleep(100)
Loop
End Sub

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The entire code-behind will look something like this:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnScroll.Enabled = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScroll.Click
btnScroll.Enabled = False
StartScroll()
End Sub

Private Sub StartScroll()
Dim sb As New System.Text.StringBuilder(textBox1.Text & " ")
Do
Dim ch As Char = sb(sb.Length - 1)
sb.Remove(sb.Length - 1, 1)
sb.Insert(0, ch)
Label1.Text = sb.ToString()
Label1.Refresh()
System.Threading.Thread.Sleep(100)
Loop
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Trim() = "" Then
btnScroll.Enabled = False
Else
btnScroll.Enabled = True
End If
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more .NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!