I am looking for an efficient way of scrolling text like a marquee in web terminology.
I managed to achieve this using a piece of code I found online:
private int xPos = 0, YPos = 0;
private void Form1_Load(object sender, EventArgs e)
{
//link label
lblText.Text = "Hello this is marquee text";
xPos = lblText.Location.X;
YPos = lblText.Location.Y;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (xPos == 0)
{
this.lblText.Location = new System.Drawing.Point(this.Width, YPos);
xPos = this.Width;
}
else
{
this.lblText.Location = new System.Drawing.Point(xPos, YPos);
xPos -= 2;
}
}
The code is very simple and it uses, a timer tick event.
It works great initially, but after scrolling 3 or 4 times, it does not reappear.
Is there anything I can adjust to make the scrolling infinite?
try:
You mentioned that it 'cuts off' if the string is longer than the form width. I assume you mean that the label jumps back to the right side of the form as soon as it hits the left side, which means you can't read the full text?
If so, you could set the 'minimum Left' of the Label to be the negative of it's width. This would allow the label to scroll fully off the form before resetting it:
Or, you can set the 'minimum Left' to be the negative of the difference between the label width and the form width, so that it would not reset until the rightmost characters have been shown:
Many other options, too. Like having multiple labels running back to back in rotation, so there is never any blank text!! You can figure out how many labels to generate dynamically (based on the difference between their width and the form's width) and handle their positions in the Timer event.
If you fill the Label by adding blanks til it has the length you want, this may look nicer:
Adding the right amount of blanks may be a bit of a challenge, though. You will need to do that on Form.Resize! Doing this right is a bit trickier, than one might think.
The perfect marquee would combine the pixelwise movement and the rollover effect, maybe by owner-drawing the label, maybe like this 'Real Marquee', so 80s ;-)
Looks really smooth and needs no outside code but Start, Stop and setting Speed and perhaps the MarqueeTimer Intervall. All regular Properties will work from the Designer, just set
AutoSize=false
and make it large enough to fill the area!