is there a way to have the text in a label scroll horizontally,if the text do not fit into the label...
please help...

Dani AI

Generated

If you mean a ticker/marquee effect (not scrollbars), a WinForms Label will clip text when AutoSize=false and can show ellipsis with AutoEllipsis=true. That solves readability, but not animation. is right that a TextBox with scrollbars is simplest for long, user-controlled text; however, if you want an auto-scrolling banner, animate the label itself rather than rewriting characters as in ’s example (rewriting can cause uneven spacing and high GC pressure for long texts).

WinForms marquee approach: place a Label inside a fixed-width Panel (acts as the viewport), and move the label left on a Timer. This keeps kerning intact and is flicker-free with double buffering.

// Designer: panelViewport (Width fixed, AutoScroll=false), labelTicker (AutoSize=true) inside it.
// Form code:
public partial class Form1 : Form
{
    readonly Timer _timer = new Timer { Interval = 30 }; // ~33 FPS
    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;               // reduce flicker
        labelTicker.Text = "This is a long message that scrolls smoothly...";
        labelTicker.Left = panelViewport.Width;   // start just off the right edge
        _timer.Tick += (s, e) =>
        {
            labelTicker.Left -= 2;                // speed in px/tick
            if (labelTicker.Right <= 0)           // fully left of viewport
                labelTicker.Left = panelViewport.Width;
        };
        _timer.Start();
    }
}

ASP.NET WebForms/page (client-side) alternative: use a clipped container and CSS animation. This avoids the deprecated <marquee> and works without JavaScript.

<style>
.view { width: 320px; overflow:hidden; white-space:nowrap; }
.ticker { display:inline-block; padding-left:100%;
          animation: scroll 12s linear infinite; }
.ticker:hover { animation-play-state: paused; } /* accessibility */
@keyframes scroll { from { transform:translateX(0); }
                    to   { transform:translateX(-100%); } }
</style>

<div class="view"><span class="ticker">Your long message goes here...</span></div>

Tips:

  • Prefer ellipsis (AutoEllipsis=true) if constant movement could hurt UX.
  • Expose a way to pause (hover or a Pause button) for accessibility.
  • For dynamic text widths, measure with TextRenderer.MeasureText(...) if you need smarter reset logic.

Recommended Answers

All 4 Replies

Scroll text ok, but what did you mean by "if the text do not fit into the label..." ?
ADDED: Text always fits to the label.

A Label is just that: a Label.
Add some scrollbars to your Form, or put your text in a TextBox and add some scrollbars there.

I did the code for scrolling text in label.
Take a look at it:

public Form1()
        {
            InitializeComponent();
            label1.Text = "abcde";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char[] chars = label1.Text.ToCharArray();
            char[] newChar = new char[chars.Length];

            int k = 0;
            for (int j = 0; j < chars.Length; j++)
            {
                if (j + 1 < chars.Length)
                    newChar[j + 1] = chars[j];
                else
                {
                    newChar[k] = chars[j];
                    k++;
                }
            }
            label1.Text = new string(newChar);
        }
public Form1()
        {
            InitializeComponent();
            label1.Text = "abcdefg";
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            char[] chars = label1.Text.ToCharArray();
            //MessageBox.Show(chars[1].ToString());
            char[] newChar = new char[chars.Length];
            int l = chars.Length;
            int k = 0;
            for (int j = 0; j < chars.Length; j++)
            {
                
                if (j+1  < chars.Length)
                    newChar[j ] = chars[j+1];
                else
                {
                    newChar[l-1] = chars[k];
                  //  k++;
                }
            }
            label1.Text = new string(newChar);
        }

thanks a lottt again.... :) mainly MITJA..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.