Marquee a Text in C#.Net 4.0 Window Form Application

Introduction

I think everyone is aware of the marquee tag in HTML that scrolls text and we all also use the marquee tag in .Net applications to scroll text but in a web application. In this article I implement this for a Windows Forms since there is not a control available to scroll the text. So in this article I will scroll the text using the timer control.

Use the following procedure to create this.

First of  take the control from the toolbox to design the window. In this article I have taken two buttons, one label and one timer control to implement the marquee in C#.

degin-of-winform.jpg

Now enter C# code for the Left to Right button click event, as in:

 private void button1_Click(object sender, EventArgs e)

 {

     xpos = label1.Location.X;

     ypos = label1.Location.Y;

     mode = "Left-to-Right";

     timer1.Start();

}

And enter it for the left button click, as in:

 private void button2_Click(object sender, EventArgs e)

 {

     xpos = label1.Location.X;

     ypos = label1.Location.Y;

     mode = "Right-to-Left";

     timer1.Start();

 }

 

Here is the full source code in C#:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace marquee_text

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private int xpos = 0, ypos = 0;

        public string mode = "Left-to-Right";

        private void button1_Click(object sender, EventArgs e)

        {

            xpos = label1.Location.X;

            ypos = label1.Location.Y;

            mode = "Left-to-Right";

            timer1.Start();

           

        }

        private void button2_Click(object sender, EventArgs e)

        {

            xpos = label1.Location.X;

            ypos = label1.Location.Y;

            mode = "Right-to-Left";

            timer1.Start();

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            if (mode == "Left-to-Right")

            {

                if (this.Width == xpos)

                {

                    this.label1.Location = new System.Drawing.Point(0, ypos);

                    xpos = 0;

 

                }

                else

                {

                    this.label1.Location = new System.Drawing.Point(xpos, ypos);

                    xpos += 2;

                }

            }

            else if (mode == "Right-to-Left")

            {

                if (xpos == 0)

                {

                    this.label1.Location = new System.Drawing.Point(this.Width, ypos);

                    xpos = this.Width;

                }

                else

                {

                    this.label1.Location = new System.Drawing.Point(xpos, ypos);

                    xpos -= 2;

                }

            }

        }

    }

}

 

 Note : Now run your application and click on the button to see the effect.

output-of-marquee.jpg

Here I have clicked on the Right to Left button.