标签在一定长度后无意中消失

时间:2013-01-03 11:19:01

标签: c# .net winforms timer label

我有一个相当大(宽度明智)的C#WinForms应用程序,它使用System.Windows.Forms.Label内的System.Windows.Forms.Panel作为选框。

System.Timers.Timer在点击事件后更新Label位置。

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

选框的功能不是问题, 当我更改Label.Text字段时,标签会消失!

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

enter image description here

当字体大小(24pt)时,似乎限制在大约2100个字符的长度。当它较小(10pt)时,字符串可以更长(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!

我不确定它是否与宽度限制或字体大小限制或表格宽度定位有关。。较小的字体和较短的字符串定位是​​正确的。因此,这不是定位误差。

5 个答案:

答案 0 :(得分:1)

我已创建了一个要检查的测试应用程序,我认为该问题与GDI+的使用以及此库使用的硬件加速有关。在我的电脑上,大于8192像素的宽度无法正确渲染(标签会在我更改文本时消失)。

设置标签的属性UseCompatibleTextRendering(使用GDI而不是GDI+)文本呈现几乎正确,但一些字形片段在底部可见标签

您必须将标签分成几个标签:

  • 尝试在不超过8192像素的块中分解标签。
  • 如果您的文字由不同时期组成,您可以为每个时期创建一个标签(它更快更容易)。

这是测试代码:

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);
            }

        }
    }
}

答案 1 :(得分:0)

您是否尝试使用AutoEllipsis属性为固定大小的标签(AutoSize false)?如果由于宽度限制或包装问题,它应该消失。

如果这不能解决问题,那么您可能需要查看定位代码。如果它在位置计算中使用标签宽度,那么由于文本更改而导致的宽度变化可能会在某些极端情况下产生一些意外。同样,固定尺寸标签(或具有一些最大尺寸)可能有所帮助。

答案 2 :(得分:0)

我也在使用这样的东西!试试这个:

label1.Size = CreateGraphics().MeasureString(label_txt, label1.Font).ToSize();

答案 3 :(得分:0)

你可以使用断点来实现这个功能。

(brkpnt)|    string some_string = working_function_that_returns_string();  
然后在调试“step in”那个函数的时候。  逐步调试它。 从汽车窗口检查变量。 它可能有LOGICAL MISTAKE,返回“”空字符串。

你要发布work_function_that_returns_string();

其他İdea。您可以将textbox.bacgorundcolor修改为none,使其看起来像标签

答案 4 :(得分:0)

我尝试解决错误:

// Try disabling the "AutoSize"-property, and use the panels' sizes
label.AutoSize = false;
label.Width = yourRedPanel.Width;
label.Height = yourRedPanel.Height;
label.TextAlign = ContentAlignment.MiddleLeft;
label.AutoEllipsis = true;

// Check that "new_X_location"-variable is not negative or 
// too big to move the label out of the viewable area
label.Location = new System.Drawing.Point(new_X_location, label.Location.Y);

// Check that some_string.Length is as great or greater than given Substring length argument
label.Text = some_string.Substring(0, 2200);

// The max font size is the biggest possible value of float
Font testFont = new Font("Arial", float.MaxValue);

// If this doesn't help, wrap your code to try-catch block
// Run the code line by line (F10), and see where it jumps to "catch",
// if there occurs errors
try
{
   // Code here
}
catch (Exception ex) { MessageBox.Show(ex.Message); }