2016-10-14 24 views
0

我有一个Windows应用程序,我从数据库中获取数据并将其绑定到标签。我使用计时器和滚动标签,当字符串大约是150个字符时,此工作正常,但当我有大约30000个字符的字符串时,它会挂起应用程序。标签中的大字符串导致应用程序挂起(C#Windows)

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

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

+0

我不认为在C#的标签被设计为容纳这么多字符。它们被设计成对另一个对象的描述,例如一个文本框,单选按钮等。最有可能的大约250个字符不是30,000 –

+0

@Kendo所以这是我的应用程序挂起的原因? –

+0

可能有更好的控件可供使用,包括Visakh V A提到了多行支持,滚动条和只读属性的文本框。它也可能是文本存储在数据库中的方式。记录越大,从数据库中检索的时间越长。在这种情况下,将文本存储为二进制文件可能对您有帮助。看看这个答案:http://stackoverflow.com/questions/26926818/best-way-to-store-large-string-in-sql-server-database –

回答

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; 
} 
+0

使用它不会挂起应用程序,但我根本无法看到文本(不滚动,不是静态的)。请问您可以指出可能的位置? –

+0

这是显示一个黑色条形滚动---------------------- –

+0

请张贴您的来源,然后我可以帮助解决它。 – nivs1978

1

而不是标签,使用文本框并根据您的需要设置ScrollBars,MultiLine和WordWrap属性。要禁用对TextBox的编辑(并因此使其表现类似于标签),请使用ReadOnly属性。

+0

采取的形式http://stackoverflow.com/questions/2906581 /标签上的滚动条 –

+0

我还需要滚动文本。可以用texttbox实现滚动吗? –

+0

是的,当然你可以http://stackoverflow.com/questions/898307/how-do-i-automatically-scroll-to-the-bottom-of-a-multiline-text-box –

相关问题