0

I am having an windows application where I am fetching data from db and binding that to a label. I am using timer and scrolling the label, this works fine when the string is around 150 characters but when I have string of around 30000 characters it just hangs out the application.

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

Why is this related to string length and causing application to hang? Thanks in advance.

Johny Bravo
  • 414
  • 9
  • 34
  • I dont think Labels in C# were designed to contain so many characters. They are designed to be descriptions of another object e.g. a textbox, radio button etc. At most probably about 250 characters not 30,000 – Master Yoda Oct 14 '16 at 08:44
  • @Kendo so is this the reason my application hangs out? – Johny Bravo Oct 14 '16 at 09:17
  • Possibly, there are better controls to use including as Visakh V A mentioned the textbox with added properties for multi-line support, scrollbars and read only. It could also be the way text is stored in your DB. The larger the record the longer it takes to retrieve from the database. In this case, storing your text as binary may help you. Have a look at this answer: http://stackoverflow.com/questions/26926818/best-way-to-store-large-string-in-sql-server-database – Master Yoda Oct 14 '16 at 10:01

2 Answers2

1

Instead of a Label, use a TextBox and set the ScrollBars, MultiLine and WordWrap properties according to your needs. To disable editing of the TextBox (and, thus, make it behave similar to a label), use the ReadOnly property.

Visakh V A
  • 320
  • 3
  • 19
  • taken form http://stackoverflow.com/questions/2906581/scrollbar-on-a-label – Visakh V A Oct 14 '16 at 08:42
  • I need to scroll the text as well. can scroll can be achieved with texttbox? – Johny Bravo Oct 14 '16 at 08:57
  • Yes of course you can http://stackoverflow.com/questions/898307/how-do-i-automatically-scroll-to-the-bottom-of-a-multiline-text-box – Visakh V A Oct 14 '16 at 09:04
  • replacing textbox does not help, it still hang the application. If the text is small it works but with large data it hangs the app – Johny Bravo Oct 14 '16 at 09:07
  • Please enable TextBoxBase.DoubleBuffered as true and see – Visakh V A Oct 14 '16 at 09:10
  • I think Johny is trying to scroll horizontal and not vertical. TextBox and Label are not designed for very long horizontal text. My above solution with the picturebox will do the trick. – nivs1978 Oct 17 '16 at 10:46
1

I guess you are trying to create a news ticker? I am not sure that labels are designed to hold such big strings. Use a picturebox instead and update your code.

Define two variables in your form class. One to hold text offset and the other to hold the graphics object for the picture box. Like this:

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

In the form onload do this:

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

Your timer should then look like this:

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;
}
nivs1978
  • 1,126
  • 14
  • 20
  • using this doesn't hang the application but I am not able to see the text at all ( not scrolling, not static).Can you please point where that might be? – Johny Bravo Oct 14 '16 at 09:41
  • It is showing a black strip scrolling ---------------------- – Johny Bravo Oct 14 '16 at 09:47
  • Please post your source, then I can help fixing it. – nivs1978 Oct 14 '16 at 10:53
  • 1
    Try to change BackColor to Color.White or change the new SolidBrush(Color.Black) to new SolidBrush(Color.Red) or something to check if the background color and text color are not the same (then you won't see anything in the picturebox). – nivs1978 Oct 17 '16 at 10:44