Monday, April 20, 2009

Marquee in Windows forms

There are scenarios where we need to use marquees in windows applications. But there are no controls introduced for this in .NET frameworks until 3.5 SP1.


I have tried some marquee controls available ready in google search and code project, but they are not thead safe in my experience i faced problems in using them in my project as it is.


Here I have given a simple code to create your own marquee that is thread safe and reasonably serves your requirement with out causing any problem.






   1:  System.Text.StringBuilder sb;

   2:          /// <summary>

   3:          /// Method called during form load. initiates MArquee items

   4:          /// </summary>

   5:          void StartMarquee()

   6:          {

   7:              //This method sets all the input for your marquee

   8:              sb = new System.Text.StringBuilder(Marquee.marqueeProperties.Text + " -- ");

   9:              //Enable the timer control 

  10:              timerMarquee.Enabled = true;

  11:              //You can simply alter the speed of the marquee

  12:              timerMarquee.Interval = Marquee.marqueeProperties.RotationSpeed;

  13:              //Change the marquee text color

  14:              txtMarqueeDisplay.ForeColor = Marquee.marqueeProperties.TextColor;

  15:              //Change the bach ground color here

  16:              txtMarqueeDisplay.BackColor = Marquee.marqueeProperties.TextBgColor;

  17:          }





The above has to be called from your windows form load where you have to have your text box control where the marquee is going to be displayed. Here my text box is named as "txtMarqueeDisplay".






   1:    /// <summary>

   2:          /// That Runs Marquee

   3:          /// </summary>

   4:          /// <param name="sender"></param>

   5:          /// <param name="e"></param>

   6:          private void timerMarquee_Tick(object sender, EventArgs e)

   7:          {

   8:              //This is the timer's tick event

   9:              char ch = sb[0];

  10:              sb.Remove(0, 1);

  11:              sb.Insert(sb.Length, ch);

  12:              //Change the 100 below to fit your requirement

  13:              if (txtMarqueeDisplay.Text.Length > 100)

  14:                  txtMarqueeDisplay.Clear();

  15:              txtMarqueeDisplay.AppendText(sb.ToString());

  16:          }





The above method the event from the timer control which is availbale from .NET 2.0
the timer is used to make sure the marque is thread safe!



Hope you got the marquee in a very simple way!!

1 comment:

  1. What is your timer interval i tried 10,100,1000 and it looks terrible

    ReplyDelete

Please leave your comments here to improve myself!