Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标签在一定长度后意外消失_C#_.net_Winforms_Timer_Label - Fatal编程技术网

C# 标签在一定长度后意外消失

C# 标签在一定长度后意外消失,c#,.net,winforms,timer,label,C#,.net,Winforms,Timer,Label,我有一个相当大(宽度方向)的C#WinForms应用程序,它在System.Windows.Forms.Panel中使用System.Windows.Forms.Label作为字幕 一个System.Timers.Timer在滴答声事件后更新标签的位置 int new_X_location = (label.Location.X + distance_invariant) % modulo; label.Location = new Point(new_X_location, label.Loc

我有一个相当大(宽度方向)的C#WinForms应用程序,它在
System.Windows.Forms.Panel
中使用
System.Windows.Forms.Label
作为字幕

一个
System.Timers.Timer
在滴答声事件后更新
标签的位置

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


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

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


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

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

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

您可以使用断点来实现该功能

(brkpnt)|    string some_string = working_function_that_returns_string();  
然后在调试“单步执行”该功能时。 一步一步地调试它。 检查自动窗口中的变量。 它可能有返回“”空字符串的逻辑错误

你必须发布工作函数,返回字符串()


其他İdea。您可以将textbox.baggorundcolor修改为“无”,使其看起来像我试图解决错误的标签:

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

我创建了一个测试应用程序来检查,我认为问题与使用
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);
            }

        }
    }
}

请发布您的问题的相关代码和/或图片,不要使用System.Timers.Timer对控件执行任何操作。将CheckForIllegalCrossThreadCalls属性设置回true。我将使用
InvokeRequired
.Invoke()
处理此问题,谢谢。这方面没有问题。只是添加了更多代码。希望它能澄清这个问题。我也有同样的问题,也有很长的标签,还有一个大帐篷。你知道发生了什么吗?事实上我想要的恰恰相反,(
AutoSize=true
AutoEllipsis=false
)。我确实试过你说的,文本没有消失,但它看起来很短,因为它没有调整大小。这似乎意味着定位没有问题。@user1934851,较长文本处的标签大小值是多少?确定阈值(标签大小或文本大小)并切换到
AutoSize=false,AutoEllipsis=true
。另一种方法是通过增加高度来允许文本换行,但这比以前的方法更繁琐。即使我删除勾号事件,使标签固定,问题仍然存在。关于“通过增加高度来包装文本”的问题,您能更具体一些吗?@user1934851,我只是想知道标签文本是否已包装,因此无法看到-因此您需要在禁用“自动大小”的情况下增加高度,以便将较长的文本包装在您设置的任何限制的最大宽度内。但从布局的角度来看,增加字幕的高度可能会成为一个问题。。。我更愿意使用省略号。我同意这是一个更容易的选择,而且我已经尝试过了,但这意味着一次移动一个字符的文本,并为字幕提供非常交错的运动。移动标签本身似乎是显而易见的选择,因为移动的分辨率可以由像素控制。除非您知道如何在标签内按像素移动文本。。。
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);
            }

        }
    }
}