6

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

A在滴答事件后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!

在此处输入图像描述

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!

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

4

5 回答 5

2

我创建了一个测试应用程序来检查,我认为问题与GDI+该库使用的硬件加速和硬件加速有关。在我的 PC 上,大于 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);
            }

        }
    }
}
于 2014-05-30T14:34:28.420 回答
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); }
于 2014-05-28T11:38:46.257 回答
0

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

label1.Size = CreateGraphics().MeasureString(label_txt, label1.Font).ToSize();
于 2013-03-05T13:50:33.397 回答
0

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

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

于 2013-01-03T11:32:31.603 回答
0

您可以对该函数使用断点。

(brkpnt)|    string some_string = working_function_that_returns_string();  

然后在调试“介入”该功能时。一步一步调试它。从汽车窗口检查变量。它可能有返回“”空字符串的逻辑错误。

你必须发布那个 working_function_that_returns_string();

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

于 2014-05-26T21:16:33.563 回答