Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label inadvertently disappearing after certain length

I have a fairly large (width wise) C# WinForms Application that uses a System.Windows.Forms.Label inside a System.Windows.Forms.Panel as a Marquee.

A System.Timers.Timer updates the Label position after a tick event.

int new_X_location = (label.Location.X + distance_invariant) % modulo;
label.Location = new Point(new_X_location, label.Location.Y);

The functionality of the marquee is not the issue, when I change the Label.Text field, the label disappears!

string some_string = working_function_that_returns_string();
label.Text = some_string; //disappears!

enter image description here

It seems to be limited to a length of about 2100 characters or so when the font size is large (24pt). When it is smaller (10pt) the string can be much longer (label.Text.Length >= 4200).

string some_string = working_function_that_returns_string();
label.Text = some_string.SubString(0,2000); //it's still visibile here.
...
label.Text = some_string.SubString(0,2200) //it's not visible!

I am not sure if it has to do with Width limitations or Font size limitations or a Form width positioning.. Positioning is correct at smaller font sizes and shorter strings. Therefore it is not a positioning error.

like image 965
Nathan McCoy Avatar asked Jan 03 '13 11:01

Nathan McCoy


1 Answers

I've created a test application to check and I think the problem is related to the use of GDI+ and the hardware acceleration this library make use of. On my PC a width greater than 8192 pixels do not renders correctly (the label disappear like in your case when I change text).

Setting the property UseCompatibleTextRendering of the label (and so using GDI and not GDI+) the text render almost correctly, but some glyphs fragments are visibile on the bottom side of the label.

You must break your label into several labels:

  • try to break up the label in chuncks not greater than 8192 pixels.
  • if your text is composed of various periods, you could create a label for each period (it is a faster and easier).

This is the test code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace LabelMaxChars
{
    public partial class Form1 : Form
    {
        private Panel pnlStrip;
        private Label lblText;
        private Timer timer;

        public Form1()
        {
            InitializeComponent();

            pnlStrip = new Panel();
            pnlStrip.Dock = DockStyle.Top;
            pnlStrip.Height = 64;
            pnlStrip.BackColor = SystemColors.ActiveCaption;
            pnlStrip.ForeColor = Color.White;

            lblText = new Label();
            lblText.Font = new Font("Microsoft Sans Serif", 12.0f, FontStyle.Regular, 
                                    GraphicsUnit.Point, ((byte)(0)));
            lblText.Location = new Point(582, 6);
            lblText.AutoSize = true;
            lblText.UseCompatibleTextRendering = true;
            lblText.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "+
                            "Sed vestibulum elit ac nunc feugiat, non varius enim commodo. "+
                            "Etiam congue, massa sollicitudin congue dapibus, odio erat blandit "+
                            "lectus, non vehicula nisi lacus sed orci. Vestibulum ante ipsum primis "+
                            "in faucibus orci luctus et ultrices posuere cubilia Curae; Donec ullamcorper "+
                            "feugiat dui, at imperdiet elit pulvinar in. Sed ac fermentum massa. "+
                            "Mauris hendrerit magna sit amet mi eleifend fringilla. "+
                            "Donec pretium augue gravida enim fermentum placerat. "+
                            "Vestibulum malesuada nisl a odio imperdiet condimentum. Sed vitae neque nulla. "+
                            "Curabitur sed facilisis odio. Integer adipiscing, ante ac cursus dignissim, "+
                            "ante sapien auctor ligula, id faucibus elit mauris nec nulla. "+
                            "Sed elementum nisl id quam convallis dictum. Nullam nulla turpis, "+
                            "elementum ac nisi in, faucibus eleifend est. ";

            lblText.Text += lblText.Text;
            lblText.Text += lblText.Text;
            lblText.Text += lblText.Text;
            lblText.Text += lblText.Text;

            Console.WriteLine("Text length {0}", lblText.Text.Length);

            pnlStrip.Controls.Add(lblText);
            this.Controls.Add(pnlStrip);

            timer = new Timer();
            timer.Interval = 10;
            timer.Enabled = true;
            timer.Tick += new EventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            --lblText.Left;
            if (lblText.Left == this.ClientSize.Width >> 1)
            {
                lblText.Text = "Nullam id nisl tortor. Donec in commodo magna. Integer dignissim vestibulum ipsum, " +
                "ac lobortis nisl faucibus ac. Pellentesque convallis placerat est, " +
                "non tempus mi scelerisque in. Sed vel aliquam tellus. " +
                "Donec tincidunt elit et imperdiet egestas. Cras vel dictum lacus. " +
                "Nullam mollis neque ac lectus congue, eget imperdiet risus feugiat. " +
                "In commodo odio quis purus scelerisque, ut vestibulum justo vulputate. " +
                "Proin sit amet facilisis libero. Donec mollis, enim at ultrices rhoncus, " +
                "quam lectus condimentum ante, a varius urna nisl rutrum mi. " +
                "Pellentesque sodales tincidunt suscipit. Cras semper sem vulputate, " +
                "ornare eros sed, fringilla libero. Sed risus turpis, mollis vitae dictum eu, " +
                "malesuada et magna. Etiam quis orci nunc. Morbi mattis ante a nibh hendrerit vehicula. ";

                lblText.Text += lblText.Text;
                lblText.Text += lblText.Text;
                lblText.Text += lblText.Text;
                lblText.Text += lblText.Text;

                Console.WriteLine("Text length {0}", lblText.Text.Length);
            }

        }
    }
}
like image 143
Marco Sacchi Avatar answered Oct 15 '22 13:10

Marco Sacchi