0

我有一个 Windows 应用程序,我从数据库中获取数据并将其绑定到标签。我正在使用计时器并滚动标签,当字符串大约 150 个字符时,这很好用,但是当我有大约 30000 个字符的字符串时,它只会挂起应用程序。

       lblMsg1.AutoEllipsis = true;
  private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (lblMsg1.Right <= 0)
                {
                    lblMsg1.Left = this.Width;
                }
                else
                    lblMsg1.Left = lblMsg1.Left - 5;

                this.Refresh();
            }
            catch (Exception ex)
            {

            }
        }

public void bindData()
{
lblMsg.Text = "Some Large text";
}

 public void Start()
        {
            try
            {
                timer1.Interval = 150;
                timer1.Start();
            }
            catch (Exception ex)
            {
                Log.WriteException(ex);
            }
        }

为什么这与字符串长度有关并导致应用程序挂起?提前致谢。

4

2 回答 2

1

我猜你正在尝试创建一个新闻代码?我不确定标签的设计目的是为了容纳这么大的字符串。请改用图片框并更新您的代码。

在表单类中定义两个变量。一个保存文本偏移量,另一个保存图片框的图形对象。像这样:

private float textoffset = 0;
System.Drawing.Graphics graphics = null;

在 onload 表单中执行以下操作:

private void Form1_Load(object sender, EventArgs e)
{
    textoffset = (float)pictureBox1.Width; // Text starts off the right edge of the window
    pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    graphics = Graphics.FromImage(pictureBox1.Image);
}

您的计时器应如下所示:

private void timer1_Tick(object sender, EventArgs e)
{
    graphics.Clear(BackColor);
    graphics.DrawString(newstickertext, new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular), new SolidBrush(Color.Black), new PointF(textoffset, 0));
    pictureBox1.Refresh();
    textoffset = textoffset-5;
}
于 2016-10-14T09:21:40.587 回答
1

使用 TextBox 代替 Label,并根据需要设置 ScrollBars、MultiLine 和 WordWrap 属性。要禁用 TextBox 的编辑(并因此使其行为类似于标签),请使用 ReadOnly 属性。

于 2016-10-14T08:42:24.513 回答