

Navigator : Home > Tutorials > Windows Programming Tutorials > ...
Introduction to WinForms - Scrolling Text in .NET (C#)
This tutorial shows how we can create a simple application to scroll user-input text, marquee-style, in C#.
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:
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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 void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "")
btnScroll.Enabled = false; else
btnScroll.Enabled = true; } private void Form1_Load(object sender, EventArgs e)
{
btnScroll.Enabled = false; } |
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 void btnScroll_Click(object sender, EventArgs e)
{
btnScroll.Enabled = false; StartScroll(); } private void StartScroll() {
System.Text.StringBuilder sb = new System.Text.StringBuilder(textBox1.Text + " "); while (true) {
char ch = 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); } } |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
The entire code-behind will look something like this:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace TextScrollEffect {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) {
if (textBox1.Text.Trim() == "")
btnScroll.Enabled = false; else
btnScroll.Enabled = true; } private void Form1_Load(object sender, EventArgs e) {
btnScroll.Enabled = false; } private void btnScroll_Click(object sender, EventArgs e) {
btnScroll.Enabled = false; StartScroll(); } private void StartScroll() {
System.Text.StringBuilder sb = new System.Text.StringBuilder(textBox1.Text + " "); while (true) {
char ch = 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); } } } } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more .NET Tutorials? Click Here!